[英]Tkinter how to correctly style a combobox scrollbar
我正在嘗試設置 combobox 小部件滾動條的樣式。 我想設置槽的顏色和大小。 我已經能夠通過更改箭頭大小參數來設置大小,如下面的代碼所示。 這是不可取的,因為它會更改所有小部件的所有垂直滾動條。 我想針對特定的小部件。
import tkinter as tk
import tkinter.ttk as ttk
class App(tk.Tk):
def __init__(self):
super().__init__()
self.rowconfigure(0, weight=1)
self.columnconfigure(0, weight=1)
self.title('Default Demo')
self.geometry('420x200')
style = ttk.Style()
style.configure('my.TCombobox', arrowsize=30)
style.configure('Vertical.TScrollbar', arrowsize=28)
# style.configure('my.TCombobox.Vertical.TScrollbar', arrowsize=28)
values = []
for idx in range(1, 50):
values.append(f'Testing-{idx}')
cbo = ttk.Combobox(self, values=values, style='my.TCombobox')
cbo.grid(ipady=5)
def main():
app = App()
app.mainloop()
if __name__ == '__main__':
main()
組合框的滾動條不能通過 python 接口直接訪問,但您可以通過 tcl 解釋器更改其樣式。
首先,為組合框的下拉列表命名:
self.tk.eval('set popdown [ttk::combobox::PopdownWindow %s]' % cbo)
然后改變滾動條的樣式:
self.tk.eval(f'$popdown.f.sb configure -style my.TCombobox.Vertical.TScrollbar')
完整示例:
import tkinter as tk
import tkinter.ttk as ttk
class App(tk.Tk):
def __init__(self):
super().__init__()
self.rowconfigure(0, weight=1)
self.columnconfigure(0, weight=1)
self.title('Default Demo')
self.geometry('420x200')
style = ttk.Style()
style.configure('my.TCombobox', arrowsize=30)
style.configure('my.TCombobox.Vertical.TScrollbar', arrowsize=28)
values = []
for idx in range(1, 50):
values.append(f'Testing-{idx}')
cbo = ttk.Combobox(self, values=values, style='my.TCombobox')
cbo.grid(ipady=5)
self.tk.eval('set popdown [ttk::combobox::PopdownWindow %s]' % cbo)
self.tk.eval(f'$popdown.f.sb configure -style my.TCombobox.Vertical.TScrollbar')
ttk.Scrollbar(self, orient='vertical').grid(row=0, column=1, sticky='ns')
if __name__ == '__main__':
app = App()
app.mainloop()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.