Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ttk主题风格的设置方法补充 #17

Open
52LY opened this issue Mar 14, 2021 · 1 comment
Open

ttk主题风格的设置方法补充 #17

52LY opened this issue Mar 14, 2021 · 1 comment

Comments

@52LY
Copy link

52LY commented Mar 14, 2021

\atk@v1.2.2\tk\theme_ttk.go

func (w *BaseWidget) StyleName() string {
    // ttk::button .b; winfo class .b  // ==> TButton
	r, _ := evalAsString(fmt.Sprintf("winfo class %v", w.id))
	return r
}

func (w *BaseWidget) StyleLookUp(name, option string) string {
    // ttk::style lookup style -option
    // ttk::style lookup 1.TButton -font  // [---> helvetica 24]
    r1, _ := evalAsString(fmt.Sprintf("ttk::style lookup %v -%v", name,option))
    return r1
}


func StyleConfigure(name string, options map[string]string) error {
    // ttk::style configure style ?-option ?value option value...? ?
    // ttk::style configure Emergency.TButton -foreground red -padding 10
    // ttk::button .b -text "Hello" -style "Fun.TButton"
    var tmp = ""
    for k,v := range options {
        tmp = tmp + "-" + k + " " + v + " "
    }
    
    return eval(fmt.Sprintf("ttk::style configure %v %v", name,tmp))
}

func StyleMap(name string, options map[string]map[string]string) error{
    // ttk::style map style ?-option { statespec value... }?
    // ttk::style map TRadiobutton -foreground [list !pressed blue pressed yellow] -background [list selected black !selected white]
    var tmp1 = ""
    var tmp2 = ""
    for k1,v1 := range options {
        tmp2 = "[list "
        tmp1 = tmp1 + "-" + k1 + " "
        for k2,v2 := range v1 {
            tmp2 = tmp2 + k2 + " " + v2 + " "
        }
        tmp2 = tmp2 + "] "
        tmp1 = tmp1 + tmp2
    }
    // fmt.Println(fmt.Sprintf("ttk::style map %v %v", name,tmp1))
    return eval(fmt.Sprintf("ttk::style map %v %v", name,tmp1))
}

例子

fmt.Println(tk.TtkTheme.ThemeIdList(),tk.TtkTheme.ThemeId()) // 返回当前平台可用ttk主题,及默认使用的ttk主题
==> [xpnative clam alt classic default winnative vista] vista
tk.TtkTheme.SetThemeId("alt") // 设置ttk主题

// 为Radiobutton控件设置一个名为”1.TRadiobutton“ 风格
tk.StyleConfigure("1.TRadiobutton",map[string]string{"font":"{consolas 10}","foreground":"red","background":"white"})

// 为Radiobutton控件设置一个名为”1.TRadiobutton“ map风格
tk.StyleMap("1.TRadiobutton",map[string]map[string]string{"foreground":{"pressed":"yellow","!pressed":"blue"},
    "background ":{"selected":"black","!selected":"white"}})

rtn1 := tk.NewRadioButton(mw,"rtn1",)
rtn2 := tk.NewRadioButton(mw,"rtn2",)
rtn3 := tk.NewRadioButton(mw,"rtn3",)

rtn1.SetNativeAttribute("style","1.TRadiobutton")
rtn2.SetNativeAttribute("style","1.TRadiobutton")
rtn3.SetNativeAttribute("style","1.TRadiobutton")

参考:
https://tkdocs.com/tutorial/styles.html
http://www.tcl-lang.org/man/tcl8.6/TkCmd/ttk_style.htm
https://tkdocs.com/shipman/ttk-map.html

@visualfc
Copy link
Owner

可以直接提交 PullRequest

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants