我有一个关于ttk风格的问题。我在把一种定制的风格应用到平纹秤上。具体来说,这意味着,我使用一个图像作为滑块。
现在,我想创建三个不同的尺度,在那里,滑块的颜色是不同的。我将这三个图片存储在一个列表self.slider = [pyimage0, pyimage1, pyimage2]中。
开始ttk样式时:
style = ttk.Style()
style.element_create('custom.Scale.trough', 'image', self.trough)
style.element_create('custom.Scale.slider', 'image', self.slider[0])
style.layout('custom.Horizontal.TScale',
[('custom.Scale.trough', {'sticky': 'we'}),
('custom.Scale.slider',
{'side': 'left', 'sticky': '',
'children': [('custom.Horizontal.Scale.label', {'sticky': ''})]
})])
style.configure('custom.Horizontal.TScale', background='#ffffff')现在我想简单地通过彩色滑块改变,当需要的时候。但是,在创建样式元素之后,如何更改滑块图像呢?
我试过:
style.configure('custom.Horizontal.TScale.custom.Scale.slider', image=self.slider[i])但它只是停留在self.slider[0]上的每一个尺度。
问候和感谢!!
编辑
class CustomScale(ttk.Scale):
def __init__(self, master=None, **kw):
kw.setdefault("orient", "horizontal")
self.variable = kw.pop('variable', DoubleVar(master))
ttk.Scale.__init__(self, master, variable=self.variable, **kw)
self._style_name = '{}.custom.{}.TScale'.format(self, kw['orient'].capitalize()) # unique style name to handle the text
self['style'] = self._style_name发布于 2021-10-12 07:43:15
您需要分别定义每种样式,以便它们有自己独特的名称。
名字是完全任意的,我不得不使用自己的图像,但它是有效的。
我扩展了答案以演示用法。
for i,a in enumerate(["red", "green", "blue"]):
style.element_create(f'{a}.Scale.trough', 'image', self.trough)
style.element_create(f'{a}.Scale.slider', 'image', self.slider[i])
style.layout(f'{a}.Horizontal.TScale',
[(f'{a}.Scale.trough', {'sticky': 'we'}),
(f'{a}.Scale.slider',
{'side': 'left', 'sticky': '',
'children': [(f'{a}.Horizontal.Scale.label', {'sticky': ''})]
})])
style.configure(f'{a}.Horizontal.TScale', background='#ffffff')
self.redslider = ttk.Scale(self.master, from_ = 0, to=255, style = "red.Horizontal.TScale")
self.redslider.grid(row = 0, column = 0, sticky = tk.NSEW)https://stackoverflow.com/questions/69536237
复制相似问题