【问题标题】:How to get a horizontal scrollbar in Tkinter?如何在 Tkinter 中获得水平滚动条?
【发布时间】:2017-12-23 18:04:15
【问题描述】:

我现在正在学习 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】:

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

    尝试替换:

    scrollbar = Scrollbar(frame1)
    

    与:

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

    【讨论】:

    • 太棒了。这样做。泰。
    • @Will.Im.Not 没问题。
    猜你喜欢
    • 1970-01-01
    • 2014-11-05
    • 1970-01-01
    • 2012-09-05
    • 1970-01-01
    • 1970-01-01
    • 2021-05-23
    • 2021-12-22
    • 2010-12-24
    相关资源
    最近更新 更多