简体   繁体   English

水平滚动条在Tkinter中不起作用

[英]The horizontal scrollbar didn't work in Tkinter

I want to create a GUI program base on tkinter. 我想基于tkinter创建一个GUI程序。 One of the widgets is Text . 其中一个小部件是Text I want to add a horizontal scrollbar in it, but it didn't work. 我想在其中添加一个水平滚动条,但它不起作用。

Where did I make a mistake? 我在哪里弄错了?

from Tkinter import *
import tkFont


class DpWin(object):

    def run(self):
        root=Tk()
        root.geometry('768x612')
        title='dp'
        root.title(title)

        xscrollbar = Scrollbar(root, orient=HORIZONTAL)
        xscrollbar.pack(side=BOTTOM, fill=X)

        yscrollbar = Scrollbar(root)
        yscrollbar.pack(side=RIGHT, fill=Y)

        text = Text(root,xscrollcommand=xscrollbar.set,yscrollcommand=yscrollbar.set)
        text.pack()

        xscrollbar.config(command=text.xview)
        yscrollbar.config(command=text.yview)
        text.insert(END,'a'*999)
        mainloop()

    def start(self):
        self.b_start.config(state=DISABLED)
        self.b_stop.config(state=ACTIVE)

    def stop(self):
        self.b_stop.config(state=DISABLED)
        self.b_start.config(state=ACTIVE)


if __name__=='__main__':
    win=DpWin()
    win.run()

I've modified your code according to here . 我根据这里修改了你的代码。 There are 2 main differences. 主要有两个不同之处。

  1. I made it so the textbox doesn't wrap. 我做到了所以文本框没有包装。 If you wrap text, there is nothing for the horizontal scrollbar to scroll to. 如果您换行文本,则水平滚动条无法滚动到任何内容。

  2. I used the grid geometry manager on a frame to keep the scrollbars and text widgets together. 我在框架上使用了网格几何管理器来将滚动条和文本小部件保持在一起。 The advantage to using .grid is that you actually get scrollbars which are the correct width/height (something you can't achieve with pack ). 使用.grid的好处是你实际上得到的滚动条是正确的宽度/高度(这是你无法用pack实现的)。

... ...

from Tkinter import *
import tkFont

class DpWin(object):
    def run(self):
        root=Tk()
        root.geometry('768x612')
        title='dp'
        root.title(title)

        f = Frame(root)
        f.pack()

        xscrollbar = Scrollbar(f, orient=HORIZONTAL)
        xscrollbar.grid(row=1, column=0, sticky=N+S+E+W)

        yscrollbar = Scrollbar(f)
        yscrollbar.grid(row=0, column=1, sticky=N+S+E+W)

        text = Text(f, wrap=NONE,
                    xscrollcommand=xscrollbar.set,
                    yscrollcommand=yscrollbar.set)
        text.grid(row=0, column=0)

        xscrollbar.config(command=text.xview)
        yscrollbar.config(command=text.yview)
        text.insert(END, 'a'*999)
        mainloop()

    def start(self):
        self.b_start.config(state=DISABLED)
        self.b_stop.config(state=ACTIVE)

    def stop(self):
        self.b_stop.config(state=DISABLED)
        self.b_start.config(state=ACTIVE)

if __name__=='__main__':
    win=DpWin()
    win.run()

There is one comment regarding making both x and y scrollbars work within the pack framework. 关于使x和y滚动条在包框架内工作,有一条评论。 Here is a minimal example: 这是一个最小的例子:

import tkinter as tk
from tkinter import X, Y, BOTTOM, RIGHT, LEFT, Y, HORIZONTAL
class TextExample(tk.Frame):
    def __init__(self, master=None):
        super().__init__()

        sy = tk.Scrollbar(self)
        sx = tk.Scrollbar(self,  orient=HORIZONTAL)
        editor = tk.Text(self, height=500, width=300, wrap='none')
        sx.pack(side=BOTTOM, fill=X)
        sy.pack(side=RIGHT, fill=Y)
        editor.pack(side=LEFT, fill=Y)
        sy.config(command=editor.yview)
        sx.config(command=editor.xview)
        self.pack()
def main():
    root = tk.Tk()
    root.geometry("800x500+0+0")
    app = TextExample(master=root)
    root.mainloop()  
if __name__ == '__main__':
    main()   

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM