【问题标题】:Changing the default behavior of tkinter widgets更改 tkinter 小部件的默认行为
【发布时间】: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 个标签。 :-)
  • 与所提问题无关,您应该认真考虑学习如何使用packgrid。使用 place 会使您的 GUI 难以维护,并且在适应不同的屏幕尺寸或分辨率以及不同的字体大小时会遇到问题。 place 作为一个通用的布局工具确实不好。

标签: python tkinter


【解决方案1】:

我已经回答了我自己的问题。像往常一样,这是一个弄清楚要搜索什么的问题。

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 正在寻找选项的路径。不是选项本身。在我设置字体的地方,我在它前面放了一个通配符(看着它但没看到它)。有效地更改所有内容的字体。

在我设置“前景”的地方我没有创建路径。

正确的代码:

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")

这将全局更改前景、背景等。

(更)正确的代码:

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 之前的路径。当我弄清楚时,我会修改这个答案)

【讨论】:

    猜你喜欢
    • 2017-01-23
    • 1970-01-01
    • 2015-04-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-04
    相关资源
    最近更新 更多