Big*_*pie 8 python state tkinter frame
我想在tkinter中创建一个带有两个Frame的GUI,并使底部框架变灰,直到某些事件发生.
下面是一些示例代码:
from tkinter import *
from tkinter import ttk
def enable():
frame2.state(statespec='enabled') #Causes error
root = Tk()
#Creates top frame
frame1 = ttk.LabelFrame(root, padding=(10,10,10,10))
frame1.grid(column=0, row=0, padx=10, pady=10)
button2 = ttk.Button(frame1, text="This enables bottom frame", command=enable)
button2.pack()
#Creates bottom frame
frame2 = ttk.LabelFrame(root, padding=(10,10,10,10))
frame2.grid(column=0, row=1, padx=10, pady=10)
frame2.state(statespec='disabled') #Causes error
entry = ttk.Entry(frame2)
entry.pack()
button2 = ttk.Button(frame2, text="button")
button2.pack()
root.mainloop()
Run Code Online (Sandbox Code Playgroud)
这是否可以,而不必单独灰显所有frame2的小部件?
我正在使用Tkinter 8.5和Python 3.3.
Big*_*pie 10
不确定它有多优雅,但我通过添加找到了解决方案
for child in frame2.winfo_children():
child.configure(state='disable')
Run Code Online (Sandbox Code Playgroud)
它循环并禁用frame2的每个子节点,并通过更改enable()基本上反转它
def enable(childList):
for child in childList:
child.configure(state='enable')
Run Code Online (Sandbox Code Playgroud)
此外,我删除,frame2.state(statespec='disabled')因为这不做我需要的,并抛出一个错误.
这是完整的代码:
from tkinter import *
from tkinter import ttk
def enable(childList):
for child in childList:
child.configure(state='enable')
root = Tk()
#Creates top frame
frame1 = ttk.LabelFrame(root, padding=(10,10,10,10))
frame1.grid(column=0, row=0, padx=10, pady=10)
button2 = ttk.Button(frame1, text="This enables bottom frame",
command=lambda: enable(frame2.winfo_children()))
button2.pack()
#Creates bottom frame
frame2 = ttk.LabelFrame(root, padding=(10,10,10,10))
frame2.grid(column=0, row=1, padx=10, pady=10)
entry = ttk.Entry(frame2)
entry.pack()
button2 = ttk.Button(frame2, text="button")
button2.pack()
for child in frame2.winfo_children():
child.configure(state='disable')
root.mainloop()
Run Code Online (Sandbox Code Playgroud)
基于@big Sharpie 解决方案,这里有 2 个通用函数,可以禁用和启用小部件的层次结构(“包含”框架)。框架不支持状态设置器。
def disableChildren(parent):
for child in parent.winfo_children():
wtype = child.winfo_class()
if wtype not in ('Frame','Labelframe','TFrame','TLabelframe'):
child.configure(state='disable')
else:
disableChildren(child)
def enableChildren(parent):
for child in parent.winfo_children():
wtype = child.winfo_class()
print (wtype)
if wtype not in ('Frame','Labelframe','TFrame','TLabelframe'):
child.configure(state='normal')
else:
enableChildren(child)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
12402 次 |
| 最近记录: |