Golang GUI Roku远程控制/Fyne框架讨论
Golang GUI Roku远程控制/Fyne框架讨论 我决定使用Fyne在Golang中重制一个旧的Python项目:Roku Remote。我认为Fyne可能是用Golang制作不使用Javascript/前端框架的GUI的最佳选择之一。
我还能在Ubuntu和Windows上编译/运行它。唯一我不喜欢的是,我认为无法编辑任何小部件或小部件内容的颜色。图标也有些有限。除此之外……我真的很喜欢Fyne,并感谢他们所有的辛勤工作。这里还有其他人使用它吗?
我观看了与Elias Naur的会议,看起来非常有前景。找到相关的文档了吗?我在外面没找到太多资料。
是的,我在我的两个个人项目中使用了fyne。
它目前还没有很多控件,并且在颜色定制方面也存在一些限制。但就目前而言,它确实是Go语言中GUI开发的最佳选择,尽管gio可能成为一个有趣的选择。
jim: 找到相关文档了吗?我在外面没找到太多信息。
文档可以在 gioui.org 模块 - gioui.org - Go Packages 上找到,并且有几个示例。此外,Elias Naur 已经开始写博客,第一篇是 Immediate Mode GUI Programming,也许他会写更多关于使用 gioui 的文章。
在Fyne中自定义小部件颜色是完全可行的,主要通过主题系统实现。以下是几种自定义颜色的方法:
1. 自定义主题示例
package main
import (
"image/color"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/app"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/widget"
"fyne.io/fyne/v2/theme"
)
type myTheme struct{}
var _ fyne.Theme = (*myTheme)(nil)
func (m myTheme) Color(name fyne.ThemeColorName, variant fyne.ThemeVariant) color.Color {
if name == theme.ColorNameBackground {
return color.NRGBA{R: 30, G: 30, B: 46, A: 255} // 深色背景
}
if name == theme.ColorNamePrimary {
return color.NRGBA{R: 137, G: 180, B: 250, A: 255} // 主色调
}
if name == theme.ColorNameButton {
return color.NRGBA{R: 166, G: 227, B: 161, A: 255} // 按钮颜色
}
return theme.DefaultTheme().Color(name, variant)
}
func (m myTheme) Font(style fyne.TextStyle) fyne.Resource {
return theme.DefaultTheme().Font(style)
}
func (m myTheme) Icon(name fyne.ThemeIconName) fyne.Resource {
return theme.DefaultTheme().Icon(name)
}
func (m myTheme) Size(name fyne.ThemeSizeName) float32 {
return theme.DefaultTheme().Size(name)
}
func main() {
myApp := app.New()
myApp.Settings().SetTheme(&myTheme{})
window := myApp.NewWindow("Roku Remote")
// 创建自定义颜色的按钮
button := widget.NewButton("Power", func() {
// Roku控制逻辑
})
// 使用容器设置背景色
content := container.NewStack(
container.NewVBox(
widget.NewLabel("Roku Remote Control"),
button,
widget.NewButton("Home", nil),
widget.NewButton("Back", nil),
),
)
window.SetContent(content)
window.Resize(fyne.NewSize(300, 400))
window.ShowAndRun()
}
2. 直接设置小部件颜色
// 使用canvas.Rectangle作为背景
rect := canvas.NewRectangle(color.NRGBA{R: 45, G: 45, B: 65, A: 255})
rect.Resize(fyne.NewSize(300, 50))
// 创建带样式的按钮
button := widget.NewButtonWithIcon("",
theme.HomeIcon(),
func() { /* Home action */ })
button.Importance = widget.HighImportance
// 自定义标签颜色
label := widget.NewLabel("Roku Device")
label.TextStyle = fyne.TextStyle{Bold: true}
3. 图标扩展方案
Fyne支持自定义图标资源:
// 加载自定义SVG图标
var homeIcon = &fyne.StaticResource{
StaticName: "home.svg",
StaticContent: []byte(`<svg>...</svg>`),
}
// 使用自定义图标
customButton := widget.NewButtonWithIcon("",
fyne.NewStaticResource("home.svg", homeIcon.StaticContent),
nil)
// 或者使用PNG资源
resource, _ := fyne.LoadResourceFromPath("icon.png")
buttonWithImage := widget.NewButtonWithIcon("", resource, nil)
4. Roku远程控制的具体实现示例
type RokuRemote struct {
app fyne.App
window fyne.Window
ip string
}
func NewRokuRemote(ip string) *RokuRemote {
app := app.New()
window := app.NewWindow("Roku Remote")
return &RokuRemote{
app: app,
window: window,
ip: ip,
}
}
func (r *RokuRemote) sendKey(key string) {
// 实现Roku按键发送逻辑
url := fmt.Sprintf("http://%s:8060/keypress/%s", r.ip, key)
http.Post(url, "", nil)
}
func (r *RokuRemote) buildUI() fyne.CanvasObject {
grid := container.NewGridWithColumns(3,
r.createButton("↑", "up"),
r.createButton("OK", "select"),
r.createButton("↓", "down"),
r.createButton("←", "left"),
r.createButton("Home", "home"),
r.createButton("→", "right"),
r.createButton("Back", "back"),
r.createButton("Power", "power"),
r.createButton("Vol+", "volumeup"),
)
return container.NewVBox(
widget.NewLabel("Roku Remote Control"),
grid,
widget.NewSeparator(),
r.createVolumeControls(),
)
}
func (r *RokuRemote) createButton(label, key string) *widget.Button {
return widget.NewButton(label, func() {
r.sendKey(key)
})
}
Fyne的颜色定制主要通过主题系统实现,对于Roku远程控制这类应用完全够用。图标方面可以通过自定义资源或使用SVG来扩展。跨平台编译使用go build即可,Windows和Ubuntu都能正常生成可执行文件。

