0

Question

How can I have a scrollbar which moves an entire Tkinter frame? Note: I am using Python 2.7.3.

Code and Explanation

I have this code for defining the scrollbar

        scrollbar = Scrollbar(soeg)
        scrollbar.pack(side=RIGHT, fill="y")

And this code for defining the textframes

        h = 0
        s = 0
        for i in dom_nodup:

            abc = dom_nodup[h]
            text = Text(soeg, bg="brown", fg="white", height="10", width="60")          
            text.insert(INSERT, "%s \n" % abc[0])
            text.insert(END, "%s \n\n\n" % abc[1])
            text.pack()
            h += 1  
            s += 1

A new textframe is created for each text entity for later easier overview (planning on having a button to show/hide the input).

The scrollbar is present but is not functional

image

xxmbabanexx
  • 8,256
  • 16
  • 40
  • 60
  • For future reference, please note which version of Python you are using (2.x or 3.x) There are fundamental differences between the two. – xxmbabanexx Mar 01 '13 at 12:31
  • Are you wanting to scroll a text widget, or scroll the whole frame which contains multiple text widgets, or should the scollbar scroll each text widget in a synchronized fashion? – Bryan Oakley Mar 01 '13 at 13:57
  • What was wrong with my answer and @BryanOakley 's? Did we not answer the question? If we did, make sure to do the green check mark next to our names. – xxmbabanexx Mar 01 '13 at 17:52
  • @xxmbabanexx: I think they are wanting to scroll the whole frame, not just a text widget. – Bryan Oakley Mar 01 '13 at 18:13
  • @BryanOakley Ok... Is that possible? I don't believe that it is. I will edit the question to reflect that. – xxmbabanexx Mar 01 '13 at 19:57
  • @xxmbabanexx: strictly speaking you can't scroll a frame. However, there are techniques that give the same effect (eg: embed the frame in a canvas, then scroll the canvas) – Bryan Oakley Mar 01 '13 at 20:17

2 Answers2

0

I would recommend that you use the ScrolledText widget. It automatically adds a scrollbar to each text widget, and has the same arguments as Text. Here is a brief example of how to do it.

from Tkinter import * #Import the Tkinter module
from ScrolledText import ScrolledText #import the scrolled text module
message = "I \n am \n scroll \n able. \n\n\n\n\n\n Yes I am!"
class Application(Frame): #Create a frame for the widgets

    def __init__(self, master):  #initialize the grid and widgets
        Frame.__init__(self,master)
        self.grid()
        self.widgets()
    def widgets(self):
        self.mytext = ScrolledText(self, width = 10) #Creates the widget
        self.mytext.grid() #Places it


root = Tk()
root.title("My Text Example")
#make my screen dimensions work

root.geometry("500x1000")
app = Application(root)

root.mainloop()

For more information, please see the Tkinterbook and this question.

Community
  • 1
  • 1
xxmbabanexx
  • 8,256
  • 16
  • 40
  • 60
0

To make a scrollbar functional you must do two things: you must tell it which scrollable widget to scroll, and you must tell the scrollable widget which scrollbar to update with the current position.

scrollbar.configure(command=text.yview)
text.configure(yscrollcommand=scrollbar.set)
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
  • I have tried both solutions and didn't succeed. The thing is (take my limited know-how into consideration); both solutions is for scrolling the individual textwidgets. I have a Toplevel frame (soeg) with multiple textwidgets inside. I am using python 2.7.3. – user2116482 Mar 01 '13 at 13:09
  • The solution works (as in the scrollbar is now moving it's bar, but not the actual window) – user2116482 Mar 01 '13 at 13:12
  • @user2116482 the actual Frame *will not move.* You can move the widgets within the frame - not the frame itself. – xxmbabanexx Mar 01 '13 at 14:09
  • Thanks xxmbabanexx. I'm not sure on my own code, think I'll redo it and update this thread again when (if) I succeed with my current setup – user2116482 Mar 01 '13 at 19:19