用户界面-Python:Tkinter活动和非活动入口空间,带checkbutton



我有一个入口空间来导入Tkinter中的数据。我希望使用复选按钮激活或取消激活此空间。当Checkbutton==1时,您可以插入数据,当Checkbutton==0时,空格变灰,您不能插入数据

from Tkinter import *
import tkMessageBox

class MainWindow(Frame):
    def __init__(self):
        Frame.__init__(self)
        self.master.title("input")
        self.master.minsize(150, 50)
        self.grid(sticky=E+W+N+S)
        top=self.winfo_toplevel()
        top.rowconfigure(0, weight=1)
        top.columnconfigure(0, weight=1)
        for i in range(1):self.rowconfigure(i, weight=1)
        self.columnconfigure(1, weight=1)
        self.label0 = Label(self, text="Data:    ") 
        self.label0.grid(row=0, column=0, padx=2)
        self.entry11 = Entry(self) 
        self.entry11.grid(row=0, column=1, pady=2, padx=2, sticky=E+W+N+S)
        self.CheckVar_theta = IntVar()
        self.check = Checkbutton(self, text="", variable=self.CheckVar_theta, onvalue = 0, offvalue = 1)
        self.check.grid(row=0, column=3, pady=0, padx=0, sticky=E+W+N+S)
if __name__=="__main__":
   d=MainWindow()
   d.mainloop()

您需要做两件事:

  1. 制作一个函数来更改每次单击复选按钮时将调用的入口框的状态。它应该看起来像这样:

    def switch(self):
        if self.CheckVar_theta.get():
            self.entry11.config(state=DISABLED)
        else:
            self.entry11.config(state=NORMAL)
    
  2. 将检查按钮的command参数分配给该功能:

    self.check = Checkbutton(self, ..., command=self.switch)
    

最后,您的代码将如下所示:

from Tkinter import *
import tkMessageBox

class MainWindow(Frame):
    def __init__(self):
        Frame.__init__(self)
        self.master.title("input")
        self.master.minsize(150, 50)
        self.grid(sticky=E+W+N+S)
        top=self.winfo_toplevel()
        top.rowconfigure(0, weight=1)
        top.columnconfigure(0, weight=1)
        for i in range(1):self.rowconfigure(i, weight=1)
        self.columnconfigure(1, weight=1)
        self.label0 = Label(self, text="Data:    ") 
        self.label0.grid(row=0, column=0, padx=2)
        self.entry11 = Entry(self) 
        self.entry11.grid(row=0, column=1, pady=2, padx=2, sticky=E+W+N+S)
        self.CheckVar_theta = IntVar()
        self.check = Checkbutton(self, text="", variable=self.CheckVar_theta,
                                 onvalue = 0, offvalue = 1, command=self.switch)
        self.check.grid(row=0, column=3, pady=0, padx=0, sticky=E+W+N+S)
    def switch(self):
        if self.CheckVar_theta.get():
            self.entry11.config(state=DISABLED)
        else:
            self.entry11.config(state=NORMAL)
if __name__=="__main__":
   d=MainWindow()
   d.mainloop()

我认为这应该有效:

from Tkinter import *
import tkMessageBox

class MainWindow(Frame):
    def __init__(self):
        Frame.__init__(self)
        self.master.title("input")
        self.master.minsize(150, 50)
        self.grid(sticky=E+W+N+S)
        top=self.winfo_toplevel()
        top.rowconfigure(0, weight=1)
        top.columnconfigure(0, weight=1)
        for i in range(1):self.rowconfigure(i, weight=1)
        self.columnconfigure(1, weight=1)
        self.label0 = Label(self, text="Data:    ") 
        self.label0.grid(row=0, column=0, padx=2)
        self.entry11 = Entry(self) 
        self.entry11.grid(row=0, column=1, pady=2, padx=2, sticky=E+W+N+S)
        self.CheckVar_theta = IntVar()
        self.check = Checkbutton(self, text="", variable=self.CheckVar_theta, onvalue = 0, offvalue = 1)
        self.check.grid(row=0, column=3, pady=0, padx=0, sticky=E+W+N+S)
        self.update()
        def update(self):
            if self.CheckVar_theta.get() == 1:
                self.entry11.config(state=NORMAL)
            else:
                self.entry11.config(state=DISABLED)
            self.master.after(100, self.update)
if __name__=="__main__":
   d=MainWindow()
   d.mainloop()

相关内容

  • 没有找到相关文章

最新更新