0

I'm new to GUI and classes and I'm a just a bit confused, when I use a button in tkinter for python it's suppose to repeat it's command when pressed. but in my program it doesn't do that. is there something wrong with me codes that might counter it? I'm trying to make a simple program that echos whatever is typed.

-Thanks

from Tkinter import *
from PIL import Image, ImageTk
import tkMessageBox

class appsMain(Frame):

    def __init__(self,parent):
        Frame.__init__(self,parent)
        self.parent=parent

        self.initUI()
    def initUI(self):
        self.parent.title("OrganizedWindows")

        self.send=Text(self,bg="white",height=3,width=35)
        self.send.place(x=17,y=235)

        self.msg=Text(self,width=35,height=12,state="disable")
        self.msg.place(x=17,y=20)

        sendbtn=Button(self,text=" Listen ",command=self.accept)
        sendbtn.place(x=305,y=240)

        self.pack(fill=BOTH, expand=1)

    def accept(self,msg):
        self.msg.configure(state="normal")
        self.msg.insert(INSERT,msg+"\n")
        self.msg.insert(INSERT,"BYE")
        self.msg.configure(state="disable")

root=Tk()
root.geometry("350x300+300+300")
app=appsMain(root)
root.mainloop()

2 Answers2

3

Your code has a few problems. The first is solved easily:

sendbtn=Button(self,text=" Listen ",command=self.accept)

doesn't work because when the button is clicked, self.accept is called with no additional arguments (accept expects 2 arguments, [self and msg], but it is only getting 1 [self]).

You can work around this with lambda:

sendbtn=Button(self,text=" Listen ",command=lambda : self.accept("some message here"))

(This is equivalent to):

def func:
    self.accept("some message here")

sendbtn=Button(self,text=" Listen ",command=func)

But, I don't know if you want to constantly add different messages ... or where they come from, so it is difficult to give a general solution at this point.

Tkinter applications happily continue to run even after exceptions are raised. It is a good idea to watch the terminal for exceptions when you're developing a Tkinter application (In this case, it pointed me right to the source of the problem).

mgilson
  • 300,191
  • 65
  • 633
  • 696
  • thank you! and can you explain exactly what lambda is? It might be of use one day. – Tommy Li-Duong Jun 30 '12 at 16:21
  • @TommyLi-Duong -- Lambda is a way of creating a function without `def`. `f = lambda x : x**2` -- `f` is a function which squares whatever you pass to it. I'll update the post to make it a little more clear. Much more info and links ( http://stackoverflow.com/questions/890128/python-lambda-why ) – mgilson Jun 30 '12 at 17:52
0

This is to better answer your Lambda comment question. Lambda is a quick, one-liner way to write a function. The variable you set it to is the same as the name of your function for def myFunction. Then you say the keyword lambda and the letter(s)/word(s) you put after the keyword lambda are just the parameters of your function. Next you put a colon (just like you would for a normal function-> def myFunction:). After that you write whatever you want the function to return. So if you wanted a function to square a given number, n, then you could write it normally like:

def square_num(n):
    return n**2

OR as a cool Lambda:

square_num = lambda n: n**2

You can also have as many parameters as you wish, just like in a normal function, so for a given number raised to the x power you could write:

raise_num = lambda n, x: n**x
Zach King
  • 798
  • 1
  • 8
  • 21