-1

I work with Tkinter and I need this the command "scrollbar". The scroll-widget is shown but I can't scroll with it. Any idea why? Here is the code I have used for scrolling:

from Tkinter import *
import Tkinter as tk
master = tk.Tk()
scrollbar = Scrollbar(master)
scrollbar.pack(side=RIGHT, fill=Y)
master.geometry('1000x500')
master.title('test')
master.mainloop()
S.Ahmed
  • 191
  • 1
  • 4
  • 12
LoveSpock
  • 15
  • 3
  • 7
  • 1
    a scrollbar doesn't just magically work when stuck into a window, you need to add a scrollable widget (such as canvas) and link the scrollbar to the canvas. this page should give you a good starting point: http://effbot.org/tkinterbook/scrollbar.htm – James Kent May 11 '17 at 10:29
  • There's nothing in your window for it to scroll. What exactly do you expect it to do? – Bryan Oakley May 11 '17 at 11:29
  • Thank you, I have look at the side. But I think I can not work with it because I want to scroll the whole windows and not only one list. scotty3785: Yeah, I know, sorry for my misspelling... -_- Bryan Oakley: I have input but I have not copy it with because I thought it is not important for the scroll function. Should I write it in? – LoveSpock May 11 '17 at 11:44
  • Not all Tkinter widgets are scrollable, you cannot scroll the window or a `Frame` for instance. If you want to scroll the whole content of your window, you will need to put all your widgets in a `Canvas` or `Text` widget (see http://stackoverflow.com/questions/3085696/adding-a-scrollbar-to-a-group-of-widgets-in-tkinter). – j_4321 May 11 '17 at 11:53
  • if you want to simplify things, there is a class for a scrollable frame here: https://github.com/JamesGKent/python-tkwidgets/blob/master/scrollableframe.py so you pack it into the root window as if it was a normal frame, and pack/grid/place widgets into it as you would a normal frame. – James Kent May 12 '17 at 13:18

2 Answers2

0

It doesn't work because there's currently nothing to scroll. You need to add a scrollable widget.

0

Try this:

try:
    from Tkinter import *
except ImportError:
    from tkinter import *

#Note that you don't have to import tkinter as tk after you have used from tkinter import *

master=Tk() #you don't have to type tk.Tk() because you have imported all of tkinter's
#commands in your main script

def myScrollcmd(event): #You need to bind this function to your canvas so that your scroll 
#bar works
    mycanvas.config(scrollregion=mycanvas.bbox('all'))   


mycanvas = Canvas(master)
mycanvas.pack(fill=BOTH, expand=True)
myFrame = Frame(mycanvas)
mycanvas.create_window((0, 0), window=myFrame, anchor=NW)
myScrollbar = Scrollbar(mycanvas, orient=VERTICAL, command=mycanvas.yview)
myScrollbar.pack(side=RIGHT, fill=Y)
mycanvas.config(yscrollcommand=myScrollbar.set)
mycanvas.bind("<Configure>", myScrollcmd)

for x in range(100):
    Label(myFrame, text="Text "+str(x)).pack()

master.mainloop()

This works for me. I use Python 3. It should work for you as well. If not, please read the documentation and search the internet for valid tkinter commands that work with your version of python. Your code will be similar to this but you will have to modify my code to satisfy your needs.

Good luck

Community
  • 1
  • 1
S.Ahmed
  • 191
  • 1
  • 4
  • 12