简体   繁体   中英

The horizontal scrollbar didn't work in Tkinter

I want to create a GUI program base on tkinter. One of the widgets is 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 ).

...

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. 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()   

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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