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

编程入门 行业动态 更新时间:2024-10-21 14:40:46
本文介绍了如何在 Tkinter 中获得水平滚动条?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

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

更多推荐

[db:关键词]

本文发布于:2023-04-30 07:18:30,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1390720.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:滚动条   水平   如何在   Tkinter

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!