如何在python tkinter中按下按钮之前使窗口状态空闲?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何在python tkinter中按下按钮之前使窗口状态空闲?相关的知识,希望对你有一定的参考价值。
我有一个带有开始按钮的窗口和一些不同帧中的小部件。现在我想让窗口空闲(禁用状态),直到点击“开始”按钮。帮我解决这个问题。我的代码如下
from tkinter import *
class Application(Frame):
def __init__(self, master):
super(Application,self).__init__(master)
self.grid()
self.create_widgets()
def create_widgets(self):
self.be=Button(self,text="start")
self.be.grid(row=1,column=1,sticky="s")
self.columnconfigure(0, pad=3)
self.columnconfigure(1, pad=3)
self.columnconfigure(2, pad=3)
self.columnconfigure(3, pad=3)
self.rowconfigure(0, pad=5)
self.rowconfigure(1, pad=5)
self.f3=Frame(self)
self.f3.grid(row=2,column=1)
fuel = Button(self.f3, text="Button",width=19)
fuel.grid(row=1, column=0)
Temp = Button(self.f3, text="Button",width=19)
Temp.grid(row=1, column=1)
Th = Button(self.f3,text="Button",width=19)
Th.grid(row=1, column=2)
Eo = Button(self.f3, text="Button",width=19)
Eo.grid(row=1, column=3)
Bh = Button(self.f3, text="Button",width=19)
Bh.grid(row=2, column=0)
Abs = Button(self.f3, text="Button",width=19)
Abs.grid(row=2, column=1)
sp = Button(self.f3, text="Button",width=19)
sp.grid(row=2, column=2)
fo = Button(self.f3, text="Button",width=19)
fo.grid(row=2, column=3)
root= Tk()
app=Application(root)
app.mainloop()
答案
您需要循环遍历小部件并逐个禁用它们,因为您无法像这样禁用整个Frame
对象。
因此,您需要使用winfo_children()
来探测子窗口小部件。
然后,您需要使用self.be
回调向command
按钮附加一个方法,以便再次启用子窗口小部件。
Full program
这是您正在寻找的计划:
'''
Created on Apr 29, 2016
@author: billal begueradj
'''
from tkinter import *
class Application(Frame):
def __init__(self, master):
super(Application,self).__init__(master)
self.grid()
self.create_widgets()
def create_widgets(self):
self.be=Button(self,text="start", command = self.enable_widgets)
self.be.grid(row=1,column=1,sticky="s")
self.columnconfigure(0, pad=3)
self.columnconfigure(1, pad=3)
self.columnconfigure(2, pad=3)
self.columnconfigure(3, pad=3)
self.rowconfigure(0, pad=5)
self.rowconfigure(1, pad=5)
self.f3=Frame(self)
self.f3.grid(row=2,column=1)
fuel = Button(self.f3, text="Button",width=19)
fuel.grid(row=1, column=0)
Temp = Button(self.f3, text="Button",width=19)
Temp.grid(row=1, column=1)
Th = Button(self.f3,text="Button",width=19)
Th.grid(row=1, column=2)
Eo = Button(self.f3, text="Button",width=19)
Eo.grid(row=1, column=3)
Bh = Button(self.f3, text="Button",width=19)
Bh.grid(row=2, column=0)
Abs = Button(self.f3, text="Button",width=19)
Abs.grid(row=2, column=1)
sp = Button(self.f3, text="Button",width=19)
sp.grid(row=2, column=2)
fo = Button(self.f3, text="Button",width=19)
fo.grid(row=2, column=3)
for child in self.f3.winfo_children():
child.configure(state = 'disable')
def enable_widgets(self):
'''Enable the child widgets'''
for child in self.f3.winfo_children():
child.configure(state = 'normal')
root= Tk()
app=Application(root)
app.mainloop()
Demo
在程序开始时,GUI将如下所示:
点击Start
按钮后:
以上是关于如何在python tkinter中按下按钮之前使窗口状态空闲?的主要内容,如果未能解决你的问题,请参考以下文章