python-3.x - 如何在 Tkinter 中获得水平滚动条?

标签 python-3.x tkinter

我目前正在学习 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

最佳答案

您正在分配水平滚动,xscrollcommand , 到垂直 scrollbar .您需要修改 Scrollbar orient选项 'horizontal'默认情况下是 'vertical' .

尝试更换:

scrollbar = Scrollbar(frame1)

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

关于python-3.x - 如何在 Tkinter 中获得水平滚动条?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47954758/

相关文章:

python - 是否可以获取 tkinter 文本中标记的数字索引(位置)?

linux - 在 Pycharm 中修改我的 Python 代码后,如何将更改部署到我的 Portainer?

python - 在 py 文件中存储和访问数据

python 2.7 - 没有名为 tkinter 的模块

python - Tkinter 使用按钮和方法动态更改标签

python - 在 python 3.6 中使用 tkinter 模块

python - 如何在 Python 的枚举中定义自定义属性(类 Javascript)

python - 使用 ffmpeg 调整大小后文件损坏

python - 具有至少 1 个唯一列值的随机 DataFrame 样本

python - 如何从组合框中启用多个值选择?