[Tkinter-discuss] Newbie needs help with entrybox

John McMonagle jmcmonagle at velseis.com.au
Thu Jun 7 00:58:46 CEST 2007


Gigs_ wrote:
> Simon Pickles wrote:
>> Hello, I am new to python and tkinter, but enjoying it so far.
>>
>> In trying to make a simple GUI I have stumbled on a problem binding an event 
>> to a function. I've got a listbox for output and an entrybox for input. How 
>> do I act on the text in the entry box when return is pressed?
>>
>> Here's my attempt:
>>
>> # GUI
>>
>> from Tkinter import*
>> import tkMessageBox
>> from time import ctime, time
>>
>> class CGUI:
>>     def __init__(self, master):
>>         frame = Frame(master, name="server")
>>         frame.pack()
>>         self.output = Listbox(frame, width=100, height=30)
>>         self.output.grid(row=0, column=0)
>>         self.inputBox = Entry(root, bd=5, width=100)
>>         self.inputBox.bind( "<KeyPress-Return>", 
>> self.__ParseInput(self.inputBox.get()) )
>>         self.inputBox.pack(side=BOTTOM, expand=YES, fill=BOTH)
>>
>>     def __ParseInput(self, s):
>>         self.output.insert(END, s)
>>
>>
>> root = Tk()
>>
>> gui = CGUI(root)
>>
>>
>> -------------------------------------------------------------------------
>>
>>

You are not far away from success.

Problem 1:  you need to call mainloop after you instanciate the class.

Problem 2:  when you bind your __ParseInput function to the 
KeyPress-Return event, you are really binding the return result rather 
than the function name

Problem 3:  the event object gets passed to your function so you need to 
supply it as an argument

Problem 4:  passing self.inputBox.get() as an argument to the 
__ParseInput function will only pass the value of the Entry widget at 
the time the binding is made, not when you trigger the event.  So you 
need to call it from within the function itself.

See below for amendments to your code:

from Tkinter import*
import tkMessageBox
from time import ctime, time

class CGUI:
     def __init__(self, master):
         frame = Frame(master, name="server")
         frame.pack()
         self.output = Listbox(frame, width=100, height=30)
         self.output.grid(row=0, column=0)
         self.inputBox = Entry(root, bd=5, width=100)
         self.inputBox.bind( "<KeyPress-Return>", self.__ParseInput)
         self.inputBox.pack(side=BOTTOM, expand=YES, fill=BOTH)

     def __ParseInput(self, event):
         self.output.insert(END, self.inputBox.get())


root = Tk()

if __name__ == '__main__':
     gui = CGUI(root)
     root.mainloop()


> 
> 
> dont use grid and pack in same parent. it will not work.
> 


Although gigs is right in saying that you cannot mix grid and pack in 
the same parent, in your case you have not.  Your container hierarchy:

root is the toplevel
frame and inputBox are packed into root
output is gridded into frame.

So there is no contention here.


Also, do not be tempted to adopt his style of inheriting Frame - your 
original approach is better.  Frame is a container widget NOT a window.


Regards,

John


More information about the Tkinter-discuss mailing list