问题
I'm coding in Python 2.7/tkinter in Windows and am putting a scrollbar on a listbar, which I can do easily enough (thanks effbot.org). However, I also want to make the scrollbar wider - it will be used on a touchscreen so the easier it is to select it, the better. I figured the width attribute would make it wider, but all it does is create some blank space. What am I doing wrong here?
Code:
from Tkinter import *
top = Tk()
scrollbar = Scrollbar(top, width=100)
scrollbar.pack(side=RIGHT, fill=Y)
listbox = Listbox(top, yscrollcommand=scrollbar.set)
for i in range(1000):
listbox.insert(END, str(i))
listbox.pack(side=LEFT, fill=BOTH)
scrollbar.config(command=listbox.yview)
top.mainloop()
Yields this:
回答1:
just a few years late to the party, but I have a method that makes the Vertical scrollbar expandable on the X-axis! (Also, due to the current time, this works on Python 2 and 3)
The trick is to create a custom style that can expand. This example is pretty useless, you wouldn't want a scrollbar quite this thick, however the concept can be used to create one you do want!
try:
import Tkinter as tkinter
import ttk
except:
import tkinter
import tkinter.ttk as ttk
root = tkinter.Tk()
root.geometry('%sx%s' % (root.winfo_screenwidth(), root.winfo_screenheight()))
root.pack_propagate(0)
textarea = tkinter.Text(root)
style = ttk.Style()
style.layout('Vertical.TScrollbar', [
('Vertical.Scrollbar.trough', {'sticky': 'nswe', 'children': [
('Vertical.Scrollbar.uparrow', {'side': 'top', 'sticky': 'nswe'}),
('Vertical.Scrollbar.downarrow', {'side': 'bottom', 'sticky': 'nswe'}),
('Vertical.Scrollbar.thumb', {'sticky': 'nswe', 'unit': 1, 'children': [
('Vertical.Scrollbar.grip', {'sticky': ''})
]})
]})
])
scrollbar = ttk.Scrollbar(root, command=textarea.yview)
textarea.config(yscrollcommand=scrollbar.set)
textarea.pack(side='left', fill='both', expand=0)
scrollbar.pack(side='left', fill='both', expand=1)
root.mainloop()
回答2:
For the scrollbar.pack(side=RIGHT, fill=Y)
do fill=BOTH
instead of the fill=Y
.
来源:https://stackoverflow.com/questions/18598182/making-a-tkinter-scrollbar-wider