所以,問題是我有幾十個具有相同配置的按鈕,我想縮短它,這樣我就不需要為每個按鈕撰寫相同的配置。
這是代碼示例:
self.Button1 = tk.Button(self.ButtonsFrame1)
self.Button1.place(relx=0.664, rely=0.275, height=40, width=94)
self.Button1.configure(activebackground="beige",
activeforeground="#000000",
background="#8c9eef",
compound='left',
foreground="#000000",
highlightbackground="#d9d9d9",
highlightcolor="black",
pady="0",
text='''button1''')
self.Button2 = tk.Button(self.ButtonsFrame2)
self.Button2.place(relx=0.824, rely=0.275, height=40, width=94)
self.Button2.configure(activebackground="beige",
activeforeground="#000000",
background="#8c9eef",
compound='left',
foreground="#000000",
highlightbackground="#d9d9d9",
highlightcolor="black",
pady="0",
text='''button2''')
正如您所看到的 configure() 的許多元素是相同的,我知道它可以更短,但我不知道如何做到這一點。你能幫助我嗎?
uj5u.com熱心網友回復:
您可以定義 aStyle然后將其應用于您的按鈕。
以下是使用Tk 主題小部件的檔案中的一些示例:
from tkinter import ttk
# change default style for every button
ttk.Style().configure("TButton", padding=6, relief="flat", background="#ccc")
btn = ttk.Button(text="Sample")
第二個例子:
# override the basic Tk widgets
from tkinter import *
from tkinter.ttk import *
style = Style()
style.configure("my.TButton", activebackground="beige",
activeforeground="#000000",
background="#8c9eef",
compound='left',
foreground="#000000",
highlightbackground="#d9d9d9",
highlightcolor="black",
pady="0")
btn1 = Button(style="my.TButton", text="button1")
btn2 = Button(style="my.TButton", text="button2")
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/474324.html
