The horizontal scrollbar didn't work in Tkinter

11,384

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()
Share:
11,384
lvshuchengyin
Author by

lvshuchengyin

Updated on June 04, 2022

Comments

  • lvshuchengyin
    lvshuchengyin almost 2 years

    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()
    
  • lvshuchengyin
    lvshuchengyin over 11 years
    Thanks for your help and advice.It help me ! I lost the "wrap=NONE" when I create the Text.I will learn how to use grid geometry manager.Thank you!
  • mgilson
    mgilson over 11 years
    Sure. Always happy to help. Good luck. (I prefer grid to pack). Some think that pack is easier, but I've always thought grid to be much more intuitive...
  • Mohammed
    Mohammed over 9 years
    @mgilson I faced the same probelm. When using the pack manager, the xscrollbar won't work whatever I did. Even if I commented the yscrollbar code. The Solution you provided above works for grid manager. However, if the code is long and pack manager has been already in use, grid manager is of no help. Using grid manager and pack mangaer causes the code to enter in an infinite negotiation. I hope you post a solution using the pack manager. Still I am stuck with this problem. Thanks in advance!