Python 将所有tk.Button对象添加到配置的列表中
所以我想将整个程序中的所有tk.Button(和entry)对象添加到列表中。(这样我可以通过迭代和配置更改每个按钮的颜色) 我正在创建一组tkinter帧,如下所示:Python 将所有tk.Button对象添加到配置的列表中,python,python-3.x,button,tkinter,tkinter-entry,Python,Python 3.x,Button,Tkinter,Tkinter Entry,所以我想将整个程序中的所有tk.Button(和entry)对象添加到列表中。(这样我可以通过迭代和配置更改每个按钮的颜色) 我正在创建一组tkinter帧,如下所示: class AppClass(tk.Tk): def __init__(self, *args, **kwargs): # Initialising Tkinter tk.Tk.__init__(self, *args, **kwargs) tk.Tk.wm_titl
class AppClass(tk.Tk):
def __init__(self, *args, **kwargs):
# Initialising Tkinter
tk.Tk.__init__(self, *args, **kwargs)
tk.Tk.wm_title(self, 'test-program')
tk.Tk.geometry(self, '800x480')
container = tk.Frame(self)
container.pack(fill='both', expand=True)
container.grid_rowconfigure(0, weight=1)
container.grid_columnconfigure(0, weight=1)
self.background_theme = '2'
self.background_theme_to_be_written_to_file = '2'
self.frames = {}
for F in (HomePage,
SecondName,
ThirdName):
frame = F(container, self)
self.frames[F] = frame
frame.grid(row=0, column=0, sticky="nsew")
frame.configure(background=background_page)
self.show_frame(HomePage)
...
想法?我知道winfo_children()可以选择页面中的所有对象-我们可以选择程序中的所有对象吗
**
目前,我正在尝试做类似的事情——奇怪的是,它只改变了屏幕上的一个按钮(有4个):
**快速更新:
我有一个部分解决方案:
self.bind("<<ShowFrame>>", self.on_show_frame)
是的,这是可能的
您只需在Tk()
窗口上调用.winfo_children()
,该窗口将返回可迭代的窗口顶层中所有小部件的列表,如下所示:
from tkinter import *
root = Tk()
def command():
for i in root.winfo_children():
i.configure(bg="pink")
entry = Entry(root)
button = Button(root, text="Ok", command=command)
entry.pack()
button.pack()
root.mainloop()
如果您有多层小部件(即,
Frame
s包含小部件,top-level
widgets),那么您需要添加某种递归,以便获得程序中的所有小部件,我们可以使用以下方法来实现这一点:
from tkinter import *
root = Tk()
def command():
x = root.winfo_children()
for i in x:
if i.winfo_children():
x.extend(i.winfo_children())
i.configure(bg="pink")
entry = Entry(root)
button = Button(root, text="Ok", command=command)
frame = Frame(root)
entry2 = Entry(frame)
top = Toplevel(root)
entry3 = Entry(top)
entry.pack()
entry2.pack()
entry3.pack()
button.pack()
frame.pack()
root.mainloop()
如果我正确理解了您的需求,您只需在类中创建一个列表,并创建一个函数来创建您的小部件并将其添加到此列表中您至少可以选择所有对象,递归地假设
winfou children
按照您所说的方式工作。您是否无法在小部件对象列表中创建小部件,类似于范围(10)中的i的:btn[i]=tk.Button(parent,text=“Button”+num)
?
def on_show_frame(self, event):
self.list_of_objects_in_parent = tk.Button.winfo_children(self)
print(self.list_of_objects_in_parent)
for entry in self.list_of_objects_in_parent:
if type(entry) == type(self.button_to_go_to_home_page):
entry.config(bg="pink")
print("working")
from tkinter import *
root = Tk()
def command():
for i in root.winfo_children():
i.configure(bg="pink")
entry = Entry(root)
button = Button(root, text="Ok", command=command)
entry.pack()
button.pack()
root.mainloop()
from tkinter import *
root = Tk()
def command():
x = root.winfo_children()
for i in x:
if i.winfo_children():
x.extend(i.winfo_children())
i.configure(bg="pink")
entry = Entry(root)
button = Button(root, text="Ok", command=command)
frame = Frame(root)
entry2 = Entry(frame)
top = Toplevel(root)
entry3 = Entry(top)
entry.pack()
entry2.pack()
entry3.pack()
button.pack()
frame.pack()
root.mainloop()