gpt4 book ai didi

python - 在 python tkinter 中按下按钮之前,如何使窗口处于空闲状态?

转载 作者:太空宇宙 更新时间:2023-11-04 00:51:30 25 4
gpt4 key购买 nike

我有一个带有开始按钮的窗口和不同框架中的一些小部件集。现在我想让窗口空闲(禁用状态)直到单击“开始”按钮。帮我解决这个问题。我的代码是如下

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()探测子部件。

然后您需要使用 command 将方法附加到 self.be 按钮回调以再次启用子小部件。

完整程序

这是您正在寻找的程序:

'''
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()

演示

在程序开始时,GUI 将如下所示:

enter image description here

点击开始按钮后:

enter image description here

关于python - 在 python tkinter 中按下按钮之前,如何使窗口处于空闲状态?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36929104/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com