python - 将所有 tk.Button 对象添加到配置列表中

标签 python python-3.x button tkinter tkinter-entry

所以我想将整个程序中的所有 tk.Button(和条目)对象添加到列表中。 (这样我就可以通过迭代和配置来更改每个按钮的颜色)

我正在创建一堆像这样的 tkinter 框架:

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)

...

app = AppClass()

app.mainloop()
app = None
exit()

想法?我知道 .winfo_children() 可以选择页面中的所有对象 - 我们可以选择程序中的所有对象吗?

**

目前我正在尝试做这样的事情 - 奇怪的是它只改变了屏幕上的一个按钮(有 4 个):

parent.list_of_objects_in_parent = tk.Button.winfo_children(parent)

print(parent.list_of_objects_in_parent)

for entry in parent.list_of_objects_in_parent:
    entry.config(bg="pink")

**快速更新:

我有一个部分解决方案:

self.bind("<<ShowFrame>>", self.on_show_frame)

...

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")

最佳答案

是的,这是可能的。

您只需在 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()

如果您有多层小部件(IE、包含小部件的FrameToplevel小部件),那么您需要添加某种递归才能获得您程序中的所有小部件,我们可以使用如下所示的方法来完成此操作:

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

关于python - 将所有 tk.Button 对象添加到配置列表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47347940/

相关文章:

python - 将 PanedWindow 分隔符 "snap"放置到某些位置,而不是只允许用户选择任何随机位置

python-3.x - BeautifulSoup 发现所有表都是空的

android - 在后台长按硬件媒体按钮

python - 如何使用 XML 存储和读取字节字符串(PyGame 图像)?我收到 ValueError 或 TypeError (无法序列化)。

Python 删除字符串中的乱码和后面的所有内容

python - ReportLab:当单元格的内容对于页面来说太长时出现 LayoutError

android - 如果单击每个按钮并应保持不活动状态,如何禁用每个按钮,以便用户在 android 中不再单击同一个按钮?

python - 为什么 pandas df.diff(2) 与 df.diff().diff() 不同?

python - 从 CMYK 转换为 RGB

ios - Facebook 登录按钮在不应该重叠的情况下重叠了事件指示器。 swift 4