Tkinter复选框背景切换

时间:2019-06-11 09:49:31

标签: python tkinter

im试图获取它,因此在选中该复选框时会将背景更改为白色(#00ffdd),而在禁用该复选框时则将变回深色(#393636)。谢谢。

试图使它成为命令,但没有用。

from tkinter import *


#At the end of this all open slides have to be x.destroy()
#this removes them till wanted again
#eg settings.destroy inventory.destroy


def helpmenu():
    #85 > 21 ^
    help1 = Label(unbox, text ="How to play:",fg = "#ffffff", bg = "#363636", font = "Helvetica 10 bold")
    help2 = Label(unbox, text ="####################################################################################~",fg = "#ffffff", bg = "#363636", font = "Helvetica 10")
    help3 = Label(unbox, text ="####################################################################################~",fg = "#ffffff", bg = "#363636", font = "Helvetica 10")
    help4 = Label(unbox, text ="####################################################################################~",fg = "#ffffff", bg = "#363636", font = "Helvetica 10")
    help5 = Label(unbox, text ="####################################################################################~",fg = "#ffffff", bg = "#363636", font = "Helvetica 10")
    help6 = Label(unbox, text ="####################################################################################~",fg = "#ffffff", bg = "#363636", font = "Helvetica 10")
    help7 = Label(unbox, text ="####################################################################################~",fg = "#ffffff", bg = "#363636", font = "Helvetica 10")
    help8 = Label(unbox, text ="####################################################################################~",fg = "#ffffff", bg = "#363636", font = "Helvetica 10")
    help9 = Label(unbox, text ="####################################################################################~",fg = "#ffffff", bg = "#363636", font = "Helvetica 10")
    help10 = Label(unbox, text ="####################################################################################~",fg = "#ffffff", bg = "#363636", font = "Helvetica 10")
    help11 = Label(unbox, text ="####################################################################################~",fg = "#ffffff", bg = "#363636", font = "Helvetica 10")
    help12 = Label(unbox, text ="####################################################################################~",fg = "#ffffff", bg = "#363636", font = "Helvetica 10")
    help13 = Label(unbox, text ="####################################################################################~",fg = "#ffffff", bg = "#363636", font = "Helvetica 10")
    help14 = Label(unbox, text ="####################################################################################~",fg = "#ffffff", bg = "#363636", font = "Helvetica 10")
    help15 = Label(unbox, text ="####################################################################################~",fg = "#ffffff", bg = "#363636", font = "Helvetica 10")
    help16 = Label(unbox, text ="####################################################################################~",fg = "#ffffff", bg = "#363636", font = "Helvetica 10")
    help17 = Label(unbox, text ="####################################################################################~",fg = "#ffffff", bg = "#363636", font = "Helvetica 10")
    help18 = Label(unbox, text ="####################################################################################~",fg = "#ffffff", bg = "#363636", font = "Helvetica 10")
    help19 = Label(unbox, text ="####################################################################################~",fg = "#ffffff", bg = "#363636", font = "Helvetica 10")
    help20 = Label(unbox, text ="####################################################################################~",fg = "#ffffff", bg = "#363636", font = "Helvetica 10")
    help21 = Label(unbox, text ="####################################################################################~",fg = "#ffffff", bg = "#363636", font = "Helvetica 10")
    help22 = Label(unbox, text ="####################################################################################~",fg = "#ffffff", bg = "#363636", font = "Helvetica 10")


    help1.place(x=0, y=30)
    help2.place(x=0, y=50)
    help3.place(x=0, y=70)
    help4.place(x=0, y=90)
    help5.place(x=0, y=110)
    help6.place(x=0, y=130)
    help7.place(x=0, y=150)
    help8.place(x=0, y=170)
    help9.place(x=0, y=190)
    help10.place(x=0, y=210)
    help11.place(x=0, y=230)
    help12.place(x=0, y=250)
    help13.place(x=0, y=270)
    help14.place(x=0, y=290)
    help15.place(x=0, y=310)
    help16.place(x=0, y=330)
    help17.place(x=0, y=350)
    help18.place(x=0, y=370)
    help19.place(x=0, y=390)
    help20.place(x=0, y=410)
    help21.place(x=0, y=430)
    help22.place(x=0, y=450)

def settings ():
    setting1 = Checkbutton(unbox, text ="oof mode",fg = "#ffffff", bg = "#363636", font = "Helvetica 10", command = unbox.configure(background="#00ffdd"))
    setting1.place(x=0, y=100)




#####################################################################################################################
unbox = Tk()
unbox.geometry ("600x480")
unbox.configure(background="#393636")
unbox.title("Dunno What Game This Is:")
unbox.resizable(0,0)

button = Button(unbox, text = "  Close:   ",fg = "#af0519", bg = "#363636", font = "Helvetica 10", command = exit)
button.place(x=0, y=1)

button1 = Button(unbox, text = "     Inventory:     ",fg = "#ffffff", bg = "#363636", font = "Helvetica 10", command = print ("test"))
button1.place(x=65, y=1)

button2 = Button(unbox, text = "       Market:      ",fg = "#ffffff", bg = "#363636", font = "Helvetica 10", command = print ("test"))
button2.place(x=168, y=1)

button3 = Button(unbox, text = "      Gambling:     ",fg = "#ffffff", bg = "#363636", font = "Helvetica 10", command = print ("test"))
button3.place(x=272, y=1)

button4 = Button(unbox, text = "      Settings:     ",fg = "#ffffff", bg = "#363636", font = "Helvetica 10", command = settings)
button4.place(x=386, y=1)

button5 = Button(unbox, text = "    Help Menu:      ",fg = "#ffffff", bg = "#363636", font = "Helvetica 10", command = helpmenu)
button5.place(x=493, y=1)





mainloop()

在选中时应更改背景颜色,而在未选中时应更改回背景颜色。实际上会永久更改背景。谢谢。

1 个答案:

答案 0 :(得分:3)

首先,尝试将您的GUI定义为一个类。它将简化对某些变量的访问,并使代码更具可读性。

要解决您的问题,必须引入将存储复选框状态的变量,以便可以检查应应用哪种颜色。这样的事情应该起作用:

def settings ():
    def setting1_checked(widget):
        if var.get():
            widget.configure(background="#00ffdd")
        else:
            widget.configure(background="#363636")

    var = IntVar()
    setting1 = Checkbutton(unbox, text ="oof mode",fg = "#ffffff", bg = "#363636", font = "Helvetica 10", command = lambda: setting1_checked(setting1), variable=var)
    setting1.place(x=0, y=100)