如何在Tkinter中获得水平滚动条?

问题描述 投票:1回答:1

我现在正在学习Tkinter。从我的书中,我得到以下代码来生成一个简单的垂直滚动条:

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

这产生了以下不错的输出:enter image description here

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

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

这是我运行时得到的:enter image description here

python-3.x tkinter
1个回答
2
投票

你将水平滚动xscrollcommand分配给垂直的scrollbar。您需要将Scrollbarorient选项修改为'horizontal',默认为'vertical'

尝试更换:

scrollbar = Scrollbar(frame1)

有:

scrollbar = Scrollbar(frame1, orient='horizontal')
© www.soinside.com 2019 - 2024. All rights reserved.