如何在 Tkinter 中获得水平滚动条? [英] How to get a horizontal scrollbar in Tkinter?

查看:57
本文介绍了如何在 Tkinter 中获得水平滚动条?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在学习 Tkinter.从我的书中,我得到了以下用于生成简单垂直滚动条的代码:

I'm learning Tkinter at the moment. From my book, I get the following code for producing a simple vertical scrollbar:

from tkinter import * # Import tkinter

class ScrollText:
    def __init__(self):
        window = Tk() # Create a window
        window.title("Scroll Text Demo") # Set title

        frame1 = Frame(window)
        frame1.pack()
        scrollbar = Scrollbar(frame1)
        scrollbar.pack(side = RIGHT, fill = Y)
        text = Text(frame1, width = 40, height = 10, wrap = WORD,
                    yscrollcommand = scrollbar.set)
        text.pack()
        scrollbar.config(command = text.yview)

        window.mainloop() # Create an event loop

ScrollText() # Create GUI

产生以下不错的输出:在此处输入图片描述

which produces the following nice output: enter image description here

但是,当我尝试以明显的方式更改此代码以获得水平滚动条时,它会产生奇怪的输出.这是我正在使用的代码

However, when I then try to change this code in the obvious way to get a horizontal scrollbar, it's producing a weird output. Here's the code I'm using

from tkinter import * # Import tkinter

class ScrollText:
    def __init__(self):
        window = Tk() # Create a window
        window.title("Scroll Text Demo") # Set title

        frame1 = Frame(window)
        frame1.pack()
        scrollbar = Scrollbar(frame1)
        scrollbar.pack(side = BOTTOM, fill = X)
        text = Text(frame1, width = 40, height = 10, wrap = WORD,
                    xscrollcommand = scrollbar.set)
        text.pack()
        scrollbar.config(command = text.xview)

        window.mainloop() # Create an event loop

ScrollText() # Create GUI

这是我运行时得到的结果:在此处输入图片描述

and here's what I get when I run this: enter image description here

推荐答案

您正在将水平滚动 xscrollcommand 分配给垂直 scrollbar.你需要修改Scrollbarorient 'horizo​​ntal' 的选项,默认为 'vertical'.

You're assigning horizontal scrolling, xscrollcommand, to a vertical scrollbar. You need to modify Scrollbar's orient option to 'horizontal' which is by default 'vertical'.

尝试更换:

scrollbar = Scrollbar(frame1)

与:

scrollbar = Scrollbar(frame1, orient='horizontal')

这篇关于如何在 Tkinter 中获得水平滚动条?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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