user1630350
user1630350

Reputation: 79

(python) tkinter scroll bar with user generated Entry fields

I'm writing a tkinter GUI (my first one) and it is to colate data from user inputs.

anyway the way I'm doing that is as follows

from Tkinter import *
root = Tk()
textoutput = []
textbox = Entry(root, width = 5) 
textinput = []

x = 0 
offset = 0
yco = 40
press = 0 

textbox.pack()
textbox.place(x = 10, y = yco + offset) 

def addtextbox():
   global textinput, x, yco, offset, press, textbox

 offset = offset + 30
 txtinput = textbox.get()
 textinput.append(txtinput)

 textbox = Entry(root, width = 5)
 textbox.place(x = 10, y = yco + offset)



add = Button(root, text = "Add box", width = 10, command = addtextbox)    

add.pack()
add.place(x = 225, y = 5)

root.mainloop()

The problem I'm having is that it gets to a certain point when there are too many text boxes to see and I need a scroll bar. I've tried using some sample code from effbot.org and some other places that I've googled but all they seem to do is scroll the background and not the text boxes themselves.

Any ideas ?

Cheers

Aaron

Upvotes: 0

Views: 3093

Answers (1)

mgilson
mgilson

Reputation: 310079

Here's a (reasonably) simple class based solution. The class is a tkinter canvas (which allows scrolling quite nicely). I use the canvas as a geometry manager so that I can place the entry widgets wherever I choose (using the create_window method).

import Tkinter as tk    

class Manager(tk.Canvas):
    def __init__(self,master=None,**kwargs):
        tk.Canvas.__init__(self,master,**kwargs)
        self.widgets=[]

    def add_entry(self):
        entry = tk.Entry(self, width = 5)
        self.create_window(0,self._ypos(), anchor = tk.N+tk.W, window = entry)
        self.widgets.append(entry)

    def get_data(self):
        for w in self.widgets:
            print (w.get())

    def _ypos(self):
        return sum(x.winfo_reqheight() for x in self.widgets)

if __name__ == "__main__":
    root = tk.Tk()
    manager = Manager(root)
    manager.grid(row=0,column=0)
    scroll = tk.Scrollbar(root)
    scroll.grid(row=0,column=1,sticky=tk.N+tk.S)
    manager.config(yscrollcommand = scroll.set)
    scroll.config(command=manager.yview)
    b = tk.Button(root, text = "add entry", command = manager.add_entry)
    b.grid(row=1,column=0)
    b2 = tk.Button(root, text = "print stuff", command = manager.get_data)
    b2.grid(row=2,column=0)
    root.mainloop()

Suggested by @BryanOakley (Thanks!) (untested)

import Tkinter as tk    

class Manager(tk.Canvas):
    def __init__(self,master=None,**kwargs):
        tk.Canvas.__init__(self,master,**kwargs)
        self.frame = tk.Frame(self)
        self.create_window(0,0,anchor=tk.N+tk.W,window=self.frame)
        self.row = 0
        self.widgets = []

    def add_entry(self):
        entry = tk.Entry(self.frame)
        entry.grid(row = self.row,column = 0)
        self.row += 1
        self.widgets.append(entry)

Now do the other stuff as before to set up the scrollbar and you should be all set.

Upvotes: 1

Related Questions