简体   繁体   中英

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. 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. Is there a way I can turn all of these .configure commands into one? 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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