List Info

Thread: Newbie needs help with entrybox




Newbie needs help with entrybox
country flaguser name
United Kingdom
2007-06-06 11:18:31
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)


------------------------------------------------------------
-------------


Your advice appreciated!

Thanks

Simon
http://www.simonpickles.c
om  --- http://www.squirtualr
eality.com

____________________________________________________________
_____
The next generation of Hotmail is here! http://www.newhotmail.co
.uk/

_______________________________________________
Tkinter-discuss mailing list
Tkinter-discusspython.org
http://mail.python.org/mailman/listinfo/tkinter-discuss

Re: Newbie needs help with entrybox
country flaguser name
United States
2007-06-06 11:36:32
I'm quite a noob to this also, but I can say that two things
are missing:  A
function that your return event would call telling Python
what to do when
the event occurs, and mainloop.  

For instance, here is a sample code that when you click on
it, will tell you
where the mouse click event occurred:

# File: bind1.py

from Tkinter import *

root = Tk()

def callback(event):     #this will be called later, the
definition of the
event.
    print "clicked at", event.x, event.y

frame = Frame(root, width=100, height=100)
frame.bind("<Button-1>", callback) 
#callback is the function, previously
defined.
frame.pack()

root.mainloop()


HTH

Teresa

-----Original Message-----
From: tkinter-discuss-bouncespython.org
[mailto:tkinter-discuss-bouncespython.org] On Behalf Of
Simon Pickles
Sent: Wednesday, June 06, 2007 9:19 AM
To: tkinter-discusspython.org
Subject: [Tkinter-discuss] Newbie needs help with entrybox

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)


------------------------------------------------------------
-------------


Your advice appreciated!

Thanks

Simon
http://www.simonpickles.c
om  --- http://www.squirtualr
eality.com

____________________________________________________________
_____
The next generation of Hotmail is here! http://www.newhotmail.co
.uk/

_______________________________________________
Tkinter-discuss mailing list
Tkinter-discusspython.org
http://mail.python.org/mailman/listinfo/tkinter-discuss



_______________________________________________
Tkinter-discuss mailing list
Tkinter-discusspython.org
http://mail.python.org/mailman/listinfo/tkinter-discuss

Re: Newbie needs help with entrybox
country flaguser name
Croatia
2007-06-06 13:22:36
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)
>
>
>
------------------------------------------------------------
-------------
>
>
> Your advice appreciated!
>
> Thanks
>
> Simon
> http://www.simonpickles.c
om  --- http://www.squirtualr
eality.com
>
>
____________________________________________________________
_____
> The next generation of Hotmail is here! http://www.newhotmail.co
.uk/
>
> _______________________________________________
> Tkinter-discuss mailing list
> Tkinter-discusspython.org
> http://mail.python.org/mailman/listinfo/tkinter-discuss
>
> __________ NOD32 2313 (20070606) Information
__________
>
> This message was checked by NOD32 antivirus system.
> http://www.eset.com
>
>
>
>   
from Tkinter import *
import tkMessageBox
from time import ctime, time

class CGUI(Frame):
    def __init__(self, master=None):
        Frame.__init__(self, master)
        self.pack()
        self.output = Listbox(self, width=100, height=30)
        self.output.pack()
        self.inputBox = Entry(self, bd=5, width=100)
        self.inputBox.pack(side=BOTTOM, expand=YES,
fill=BOTH)
        self.inputBox.bind("<Return>",
lambda event: 
self.parseInput(self.inputBox.get()))

    def parseInput(self, s):
        self.output.insert(END, s)
        self.inputBox.delete(0, END)

CGUI().mainloop()


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

i explain more but my english is bad and dont know how to
express myself
_______________________________________________
Tkinter-discuss mailing list
Tkinter-discusspython.org
http://mail.python.org/mailman/listinfo/tkinter-discuss

Re: Newbie needs help with entrybox
user name
2007-06-06 17:58:46
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
_______________________________________________
Tkinter-discuss mailing list
Tkinter-discusspython.org
http://mail.python.org/mailman/listinfo/tkinter-discuss

[1-4]

about | contact  Other archives ( Real Estate discussion Medical topics )