【问题标题】:Python/Tkinter - Change the Text Color Of on a Checkbox, When Checkbox is Checked?Python/Tkinter - 在选中复选框时更改复选框上的文本颜色?
【发布时间】:2019-01-20 22:47:43
【问题描述】:

正如标题所暗示的那样...有没有办法在选中复选框时更改复选框上的文本颜色,然后在取消选中复选框时返回默认颜色?

【问题讨论】:

    标签: python checkbox text tkinter colors


    【解决方案1】:

    是的,当Checkbutton 开启时,您确实可以更改文本的颜色。要更改文本的颜色,请使用选项fg。我创建了两个变量off_coloron_colorCheckbutton关闭时,文字颜色为red按钮,打开时颜色为green
    这是一个代码:

    from tkinter import *
    import tkinter
    root = Tk()
    off_color = "red"
    on_color = "green"
    def on_check():    #this function will run on click on checkbutton
        if cbVar.get() == "1":chbox["fg"] = on_color     # if (get current checkbutton state) is "1" then....
        else:chbox["fg"] = off_color
    cbVar = StringVar(root)     #making variable for checkbutton
    cbVar.set(0)          #turning off the checkbutton (initialy)
    chbox = Checkbutton(root,variable = cbVar,text="Check me",command=on_check,fg=off_color)     #making the checkbutton
    chbox.place(x=0,y=0)         #placing the checkbutton
    mainloop()
    

    checkbutton 关闭时:

    checkbutton 开启时:

    【讨论】:

    • 如果你不介意我问... 'cb.Var.set(0)' 有什么作用?它是否确保复选框在启动时处于关闭状态?
    • @Est cb.Var.set(0) 关闭检查按钮。
    猜你喜欢
    • 1970-01-01
    • 2020-09-21
    • 2013-02-19
    • 1970-01-01
    • 1970-01-01
    • 2021-05-24
    • 1970-01-01
    • 1970-01-01
    • 2015-11-17
    相关资源
    最近更新 更多