如何根据使用tkinter的复选框禁用条目?

时间:2018-05-07 14:00:59

标签: python checkbox tkinter

我在Python中使用tkinter来创建GUI。我希望用户能够给出最大值和最小值,如果它是一个常量值,只输入该值。我希望通过实现一个他们可以勾选的复选框,然后最大值框显示为灰色。下面是我的代码

from tkinter import *

class GUI(object):
    def __init__(self, master):
        self.master = master
        master.title("Variabelen")
        Label(master, text="Min").grid(row=0, column=1)
        Label(master, text="Max").grid(row=0, column=2)
        Label(master, text="Vaste waarde").grid(row=0, column=3)

        Label(master, text="Oppervlakte chiller").grid(row=1)
        Label(master, text="Diameter buizen").grid(row=2)

        aChillerMin = Entry(master)
        aChillerMax = Entry(master)
        dMin = Entry(master)
        dMax = Entry(master)

        aChillerMin.grid(row=1, column=1)
        aChillerMax.grid(row=1, column=2)
        aChillerVast = IntVar()
        chk = Checkbutton(root, variable=aChillerVast).grid(row = 1, column = 3)

        if aChillerVast.get():
           aChillerMax.config(state=DISABLED)

        dMin.grid(row=2, column=1)
        dMax.grid(row=2, column=2)

root = Tk()
myGUI = GUI(root)
root.mainloop()

1 个答案:

答案 0 :(得分:1)

因此,您希望在checkbutton为True时禁用该条目,否则禁用它(如果不是则在注释中更正我)。为此,在checkbutton小部件中给出一个命令。

您就是这样做的:

from tkinter import *

class GUI(object):
    def __init__(self, master):
        self.master = master
        master.title("Variabelen")
        Label(master, text="Min").grid(row=0, column=1)
        Label(master, text="Max").grid(row=0, column=2)
        Label(master, text="Vaste waarde").grid(row=0, column=3)

        Label(master, text="Oppervlakte chiller").grid(row=1)
        Label(master, text="Diameter buizen").grid(row=2)

        aChillerMin = Entry(master)
        aChillerMax = Entry(master)
        dMin = Entry(master)
        dMax = Entry(master)

        aChillerMin.grid(row=1, column=1)
        aChillerMax.grid(row=1, column=2)
        aChillerVast = IntVar()

        def activateCheck():
            if aChillerVast.get() == 1:          #whenever checked
                aChillerMax.config(state=NORMAL)
            elif aChillerVast.get() == 0:        #whenever unchecked
                aChillerMax.config(state=DISABLED)

        chk = Checkbutton(root, variable=aChillerVast, command=activateCheck).grid(row = 1, column = 3)    #command is given

        aChillerMax.config(state=DISABLED)

        dMin.grid(row=2, column=1)
        dMax.grid(row=2, column=2)

root = Tk()
myGUI = GUI(root)
root.mainloop()

希望这会有所帮助。