import (
. "modernc.org/tk9.0"
_ "modernc.org/tk9.0/themes/azure"
)
Initialize() 初始化
lbl := TLabel(Txt("中国"), Justify("center"))
sharedVar := Variable("默认值")
radio := TRadiobutton(Txt("中国"), sharedVar, Value("1"))
radio2 := TRadiobutton(Txt("日本"), sharedVar, Value(2))
display := TLabel(Background(White), Width(15))
Pack(display,
lbl,
radio, radio2,
TButton(Txt("Read Value"), Command(func() {
display.Configure(Txt(radio.Variable()))
lbl.Configure(Txt(sharedVar.Get()))
})),
TExit(),
Padx("1m"), Pady("2m"), Ipadx("1m"), Ipady("1m"))
ActivateTheme("azure light")
App.SetResizable(true, true)//禁止最大化设置false false
App.Wait()
鼠标按键
鼠标左键单机 鼠标右键单机 左键单双击 鼠标左键移动 左右shift 回车 ctrl+f 任意键按下 绑定右键菜单到控件
label1 := TLabel(Txt("菜单")) menu := Menu() ex1 := menu.AddCommand(Lbl("显示1"), Command(func(e *Event) { MessageBox(Msg("ok1")) })) menu.AddCommand(Lbl("显示2"), Command(func(e *Event) { MessageBox(Msg("ok2")) })) Bind(label1, "<Button-3>", Command(func(e *Event) { Popup(menu.Window, e.XRoot, e.YRoot, ex1) }))
左上角图标,支持ico和png格式
//go:embed testico.ico
var ico []byte
App.IconPhoto(NewPhoto(Data(ico)))
- 显示图像,图片上写字
c := Canvas(Background(Red), Width(200), Height(200), Highlightcolor(Red))
img := NewPhoto(Data(ico))
c.CreateImage(100, 100, Image(img))
Label(Image(NewPhoto(Data(ico)))) 显示图像
Label(Image(NewPhoto(Data(svg)))) 支持svg代码图片
c.CreateText(200, 200, Fill(Red), Justify("center"),
Txt("我是中国人.\n我爱美国"), Font(HELVETICA, 16, "bold"))
btn := TButton(Txt("中国"))
btn.Txt() 获取内容
co := ChooseColor(Initialcolor("red"), Title("选择颜色"))
fmt.Println(co) #ff0000,参数是默认颜色,返回字符串
s := GetOpenFile(
Title("打开文件"),
Initialdir(uhome),
Multiple(true),
Filetypes([]FileType{{"Go 文件", []string{".go"}, ""}}))
fmt.Println(s)
GetSaveFile(
Title("保存文件"),
Confirmoverwrite(true),
Filetypes([]FileType{{"Go files", []string{".go"}, ""}}))
home, _ := os.UserHomeDir() //默认目录,
co := ChooseDirectory(Initialdir(home))
fmt.Println(co) C:/Users/yoby/
ClipboardAppend("添加到剪贴板")
r := ClipboardGet()
if Error != nil {
r = "<empty>"
}
fmt.Println(r) 获取剪贴板
lbl := Label()
NewTicker(100*time.Millisecond, func() {
lbl.Configure(Txt(time.Now().Format(time.DateTime)))
})
动态显示时间
Destroy(App) 退出
TExit(Txt("退出"))
Bind(App, "<KeyPress>", Command(func(e *Event) {
fmt.Printf("click %+v\n", e)
})) 绑定任意键
b1 := TButton(Txt("Hello"), Command(func(e *Event) {
fmt.Printf("click %+v\n", e)
})) 按钮事件
- 滚动显示
fontSize := int(10*TkScaling()/NativeScaling + 0.5) font := Font("helvetica", fontSize) var scroll *TScrollbarWidget t := Text(font, Height(4), Yscrollcommand(func(e *Event) { e.ScrollSet(scroll) }), Setgrid(true), Wrap("word"), Padx("4p"), Pady("6p")) scroll = TScrollbar(Command(func(e *Event) { e.Yview(t) })) Grid(t, Sticky("news"), Pady("1m")) Grid(scroll, Row(0), Column(1), Sticky("nes"), Pady("1m")) t.InsertML(`文章介绍了Tkinter中如何<br><br><br><br><br><br>通过bind方法绑定各种事件`)
- 列表单选
myList := Listbox() myList.Insert(0, "Choice1", "Choice2", "Choice3") btn := TButton(Txt("提交"), Command(func() { str := myList.Get("active") fmt.Println(str[0]) }))
- 下载进度条
pb := TProgressbar(Length("400")) var start *TButtonWidget lbl := TLabel(Txt("0%")) start = TButton(Txt("下载"), Command(func() { start.Configure(State("disabled")) //disabled禁用 enabled启用 pb.Configure(Value(100)) //进度100是满进度,0是开始 })) Grid(pb) Grid(start) Grid(lbl)
- 表格扩展
. "modernc.org/tk9.0/extensions/tablelist"
InitializeExtension("tablelist")
style := Opts{Padx("1m"), Pady("2m"), Ipadx("2m"), Ipady("1m")}
t := Tablelist0(`.t -columns {0 "First Column" 0 "Another column"}`)
t.Do(`insert end [list "first row" "another value"]`)
t.Do(`insert end [list "another row" "bla bla"]`)
t.Do(`insert end [list "more rowz" "ha ha ha"]`)
Grid(t, Sticky("news"), style)
- 提示
Grid(Tooltip(TLabel(Txt("Tooltip Label")), "This is a label"))
- 窗口最顶端
WmAttributes(App, Topmost(true))
- 2秒后执行
TclAfter(time.Second*2, func() { fmt.Println(1) })
作者:Yoby 创建时间:2025-02-26 23:40
最后编辑:Yoby 更新时间:2025-02-27 23:35
最后编辑:Yoby 更新时间:2025-02-27 23:35