简体   繁体   English

如何将TKinter中的.configure压缩为一个命令?

[英]How can I condense a .configure in TKinter down to one command?

I am creating a program which will use multiple menus and buttons, and to make the menus quick (and as I am not very proficient with Tkinter) I have used PAGE to generate some code for me. 我正在创建一个程序,该程序将使用多个菜单和按钮,并使菜单快速(并且由于我对Tkinter并不十分熟练),所以我使用PAGE为我生成了一些代码。 However, it outputs a very lengthy set of .configure statements for each button, meaning my project now totals over 1400 lines of code- and I'm not even halfway done yet. 但是,它为每个按钮输出一组非常长的.configure语句,这意味着我的项目现在总计超过1400行代码-而且我还没有完成一半。 Is there a way I can turn all of these .configure commands into one? 有没有办法将所有这些.configure命令合而为一? I have included a sample of my code below for reference. 我在下面提供了我的代码示例,以供参考。

self.Button7 = Button(top)
self.Button7.place(relx=0.04, rely=0.76, height=24, width=257)
self.Button7.configure(activebackground="#d9d9d9")
self.Button7.configure(activeforeground="#000000")
self.Button7.configure(background="#d9d9d9")
self.Button7.configure(command=root.destroy)
self.Button7.configure(disabledforeground="#a3a3a3")
self.Button7.configure(foreground="#000000")
self.Button7.configure(highlightbackground="#d9d9d9")
self.Button7.configure(highlightcolor="black")
self.Button7.configure(pady="0")
self.Button7.configure(text='''Go back''')

You can put them all in a single command: 您可以将它们全部放在一个命令中:

self.Button7.configure(foreground="#000000", highlightbackground="#d9d9d9", highlightcolor="black", etc)

But why would you want to? 但是为什么要呢? It's much neater to do it your way. 按照自己的方式进行操作更整洁。

You can create a dictionary of the arguments that need to be passed for configuration and then use the argument unpacking magic ** , like below. 您可以创建需要传递的参数dictionary来进行配置,然后使用参数解压缩magic ** ,如下所示。

my_config = {
    'foreground': "#000000",
    'background': "#d9d9d9",
    # ...
}
self.Button7.configure(**my_config)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM