更改 tkinter 小部件的默认行为 [英] Changing the default behavior of tkinter widgets
问题描述
我想更改各种小部件的默认行为.特别是我想将标签的默认浮雕设置为 SUNKEN,默认背景设置为灰色,默认前景设置为蓝色等......
I would like to change the default behaviors of various widgets. In particular I want to set the default relief of labels to SUNKEN, the default background to grey, the default foreground to blue, etc....
我尝试了 add_option,但唯一需要的是*font"、arial 32".
I have tried add_option, but the only thing that took was "*font","arial 32".
我希望这样做,因为我有一个屏幕,上面有很多标签,并且代码变得混乱,一行一行一行的浮雕=SUNKEN.
I wish to do this as I have one screen that has many labels on it and the code gets messy with the same relief=SUNKEN line after line.
谢谢
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)
推荐答案
我已经回答了我自己的问题.像往常一样,这是一个弄清楚要搜索什么的问题.
I have answered my own question. As usual it is a matter of figuring out what to search for.
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")
option_add 正在寻找选项的路径.不是选项本身.在我设置字体的地方,我在它前面放了一个通配符(直视它但从未见过它).有效地更改所有内容的字体.
option_add is looking for a path to the option. Not the option itself. Where I set the Font, I placed a wildcard in front of it (looked right at it but never saw it). Effectively changing the Font for everything.
在我设置前景"的地方我没有创建路径.
Where I set the 'foreground' I did not create a path.
正确的代码:
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")
这将全局更改前景、背景等.
This will change foreground,background, etc, globally.
(更多)正确的代码:
self.PartInputFrame.option_add("*Label.Font","arial 32")
self.PartInputFrame.option_add("*Label.foreground","white")
self.PartInputFrame.option_add("*Label.background","blue")
self.PartInputFrame.option_add("*Label.relief","SUNKEN")
将更改标签的默认值(全局)(还是不太明白 *Label 之前的路径.等我弄清楚了我会修改这个答案)
will change defaults for Labels (globally) ( still do not quite understand the path before *Label. When I figure that out I will amend this answer)
这篇关于更改 tkinter 小部件的默认行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!