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

2024-09-29 21:45:23 发布

您现在位置:Python中文网/ 问答频道 /正文

我在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()

Tags: textselfmastertkinterguicolumnrootlabel
1条回答
网友
1楼 · 发布于 2024-09-29 21:45:23

所以,当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()

希望这有帮助。在

相关问题 更多 >

    热门问题