【发布时间】:2014-05-22 22:18:05
【问题描述】:
我想更改各种小部件的默认行为。 特别是我想将标签的默认浮雕设置为 SUNKEN,默认背景为灰色,默认前景为蓝色等......
我尝试过 add_option,但唯一使用的是“*font”、“arial 32”。
我希望这样做,因为我有一个屏幕上有很多标签,并且代码变得一团糟,一行一行地使用相同的浮雕=SUNKEN。
谢谢
self.PartInputFrame=Frame(self.parent,width=self.parent.winfo_screenwidth(),height=self.parent.winfo_screenheight())
self.PartInputFrame.pack_propagate(0)
self.PartInputFrame.config(background='blue')
self.PartInputLabel=Label(self.PartInputFrame,anchor=CENTER)
self.PartInputLabel.config(fg='white',bg='blue')
self.PartInputLabel.config(text='Part Program Input Information')
self.PartInputLabel.place(rely=.1,relx=.5,anchor=CENTER)
self.PartInputLabel.config(font=("arial",32))
self.PartInputFrame.option_add("*Font","arial 32")
self.PartInputFrame.option_add("foreground","white")
self.PartInputFrame.option_add("background","blue")
self.PartInputFrame.option_add("relief","SUNKEN")
Label(self.PartInputFrame,text="Polyline").place(rely=.3,relx=.1)
Label(self.PartInputFrame,text='"X" Origin Set').place(rely=.35,relx=.1)
Label(self.PartInputFrame,text='Pattern(s) Long').place(rely=.4,relx=.1)
Label(self.PartInputFrame,text='Pattern(s) Wide').place(rely=.45,relx=.1)
Label(self.PartInputFrame,text='Repeat Length').place(rely=.5,relx=.1)
Label(self.PartInputFrame,text='Repeat Width').place(rely=.55,relx=.1)
Label(self.PartInputFrame,text='Mirror (Y or N)').place(rely=.6,relx=.1)
Label(self.PartInputFrame,text='Pattern Type').place(rely=.65,relx=.1)
Label(self.PartInputFrame,text='Part Program').place(rely=.3,relx=.5)
Label(self.PartInputFrame,text='Part Drawing Number').place(rely=.35,relx=.5)
Label(self.PartInputFrame,text='Plates Produced').place(rely=.40,relx=.5)
Label(self.PartInputFrame,text='Plates Remaining').place(rely=.45,relx=.5)
Label(self.PartInputFrame,text='Left Setup').place(rely=.50,relx=.5)
Label(self.PartInputFrame,text='Right Setup').place(rely=.55,relx=.5)
Label(self.PartInputFrame,text='Plate Lenght').place(rely=.60,relx=.5)
Label(self.PartInputFrame,text='Plate Width').place(rely=.65,relx=.5)
【问题讨论】:
-
option_add是更改 tkinter 小部件默认属性的正确方法。如果这对您不起作用,则您可能做错了什么。如果没有看到您的所作所为,我们将很难提供帮助。 -
这里有一些不工作的代码。字体在变化,但没有其他变化。
-
您不需要在示例中显示所有这些标签,一两个就足够了,并使您的代码更容易理解。
-
如果我不包括它们,其他人会问我为什么要做我想做的事情,只需要 2 或 3 个标签。 :-)
-
与所提问题无关,您应该认真考虑学习如何使用
pack或grid。使用 place 会使您的 GUI 难以维护,并且在适应不同的屏幕尺寸或分辨率以及不同的字体大小时会遇到问题。place作为一个通用的布局工具确实不好。