How to get a horizontal scrollbar in Tkinter?

Wil*_*Not 3 tkinter python-3.x

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
Run Code Online (Sandbox Code Playgroud)

which produces the following nice output: 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
Run Code Online (Sandbox Code Playgroud)

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

Nae*_*Nae 6

您正在将水平滚动 , 分配给xscrollcommand垂直scrollbar。您需要修改默认情况下Scrollbarorient选项。'horizontal''vertical'

尝试更换:

scrollbar = Scrollbar(frame1)
Run Code Online (Sandbox Code Playgroud)

和:

scrollbar = Scrollbar(frame1, orient='horizontal')
Run Code Online (Sandbox Code Playgroud)