使 tkinter 滚动条变宽 [英] Making a tkinter scrollbar wider

查看:71
本文介绍了使 tkinter 滚动条变宽的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Windows 中使用 Python 2.7/tkinter 进行编码,并在列表栏上放置了一个滚动条,这很容易做到(感谢 effbot.org).但是,我也想让滚动条更宽 - 它将在触摸屏上使用,因此选择它越容易越好.我认为 width 属性会使它更宽,但它所做的只是创建一些空白空间.我在这里做错了什么?

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?

代码:

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

产生这个:

推荐答案

刚参加聚会晚了几年,但我有一个方法可以使垂直滚动条在 X 轴上展开!(此外,由于当前时间,这适用于 Python 2 和 3)

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

这篇关于使 tkinter 滚动条变宽的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆