简体   繁体   English

如何以正确的方式在 ttk.scale 上使用 ttk.style?

[英]How do I use ttk.style on ttk.scale the right way?

I am trying to customize a ttk.Scale using a ttk.Style.我正在尝试使用 ttk.Style 自定义 ttk.Scale。 I don't really know, how to apply my Style to my Scale.我真的不知道,如何将我的样式应用到我的比例。 Right now I am using this:现在我正在使用这个:

style = ttk.Style()
style.configure(".", background="#505050")
scale = ttk.Scale(self.root, from_ = 100, to = 0, orient=tk.VERTICAL)

This works just fine, but I don't really understand, whats going on with the ".".这工作得很好,但我真的不明白,“。”是怎么回事。

I also tried something like this:我也尝试过这样的事情:

style = ttk.Style()
style.configure("myStyle", background="#505050")
scale = ttk.Scale(self.root, from_ = 100, to = 0, orient=tk.VERTICAL, style="myStyle")

But I am getting this error:但我收到此错误:

Layout Vertical.myStyle.TScale not found

This seems to work for buttons for example, but apparently not for scales:例如,这似乎适用于按钮,但显然不适用于比例:

style = ttk.Style()
style.configure("myStyle", background="#505050")
scale = ttk.Scale(self.root, from_ = 100, to = 0, orient=tk.VERTICAL, style="myStyle.TScale")

But I am also getting the same error.但我也遇到了同样的错误。

All in all I am pretty confused on how to handle ttk.Style with ttk.Scale.总而言之,我对如何使用 ttk.Scale 处理 ttk.Style 感到很困惑。

How do I apply a ttk.Style to a ttk.Scale the right way?如何以正确的方式将 ttk.Style 应用于 ttk.Scale?

You just need to put the "TScale" and it will work您只需要放置“TScale”就可以了

from tkinter import ttk, Tk

window = Tk()
style = ttk.Style()
style.configure("TScale", background="#505050")
scale = ttk.Scale(window, from_=0, to=100, value=0, orient="vertical", style="TScale")
scale.pack()

window.mainloop()

If you're styling a horizontal slider, use如果您正在设计水平 slider,请使用

style.configure('myStyle.Horizontal.TScale', background="red")

If you're styling a vertical slider, use如果您正在设计垂直 slider,请使用

style.configure('myStyle.Vertical.TScale', background="red")

Now myStyle acts as a custom style and can be attached to a slider to modify the looks of the respective slider object (Horizontal or Vertical).现在 myStyle 充当自定义样式,可以附加到 slider 以修改相应 slider object(水平或垂直)的外观。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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