[Tkinter-discuss] Buttonwidget, problem with callback-function!?
Martin Franklin
mfranklin1 at gatwick.westerngeco.slb.com
Mon Apr 7 09:22:39 CEST 2008
globalrev wrote:
> in the following program i want "1" to appear in the upper "entry" when i
> click the button "1".
>
> so i will replace state=DISBALED with command=self.Display(xbtn) or
> command=Display(xbtn). Display being a function that takes the pressed
> buttons "value" and so outputs in the first entry the corresponding integer
> or arithmetic.
> ive tried this but i dont get ti to work.
>
> is Display some sort of predefined function that im f***ing around with?
>
> 2nd code is the implementation of the Display-funcand corresponding
> errormessages.
>
>
> what is the problem here?
>
>
>
> #! /usr/bin/env python
> from Tkinter import *
> import tkMessageBox
>
> class GUIFramework(Frame):
> """This is the GUI"""
>
> def __init__(self,master=None):
> """Initialize yourself"""
>
> """Initialise the base class"""
> Frame.__init__(self,master)
>
> """Set the Window Title"""
> self.master.title("Calculator")
>
> """Display the main window"
> with a little bit of padding"""
> self.grid(padx=10,pady=10)
> self.CreateWidgets()
>
> def CreateWidgets(self):
>
> self.btnDisplay = Button(self, text="1", state=DISABLED)
> self.btnDisplay.grid(row=0, column=0, padx=5, pady=5)
>
> self.btnDisplay = Button(self, text="2", state=DISABLED)
> self.btnDisplay.grid(row=0, column=1, padx=5, pady=5)
>
> self.btnDisplay = Button(self, text="3", state=DISABLED)
> self.btnDisplay.grid(row=0, column=2, padx=5, pady=5)
>
> self.btnDisplay = Button(self, text="+", state=DISABLED)
> self.btnDisplay.grid(row=0, column=3, padx=5, pady=5)
>
> self.btnDisplay = Button(self, text="4", state=DISABLED)
> self.btnDisplay.grid(row=1, column=0, padx=5, pady=5)
>
> self.btnDisplay = Button(self, text="5", state=DISABLED)
> self.btnDisplay.grid(row=1, column=1, padx=5, pady=5)
>
> self.btnDisplay = Button(self, text="6", state=DISABLED)
> self.btnDisplay.grid(row=1, column=2, padx=5, pady=5)
>
> self.btnDisplay = Button(self, text="-", state=DISABLED)
> self.btnDisplay.grid(row=1, column=3, padx=5, pady=5)
>
> self.btnDisplay = Button(self, text="7", state=DISABLED)
> self.btnDisplay.grid(row=2, column=0, padx=5, pady=5)
>
> self.btnDisplay = Button(self, text="8", state=DISABLED)
> self.btnDisplay.grid(row=2, column=1, padx=5, pady=5)
>
> self.btnDisplay = Button(self, text="9", state=DISABLED)
> self.btnDisplay.grid(row=2, column=2, padx=5, pady=5)
>
> self.btnDisplay = Button(self, text="*", state=DISABLED)
> self.btnDisplay.grid(row=2, column=3, padx=5, pady=5)
>
> self.btnDisplay = Button(self, text="0", state=DISABLED)
> self.btnDisplay.grid(row=3, column=0, padx=5, pady=5)
>
> self.btnDisplay = Button(self, text="C", state=DISABLED)
> self.btnDisplay.grid(row=3, column=1, padx=5, pady=5)
>
> self.btnDisplay = Button(self, text="r", state=DISABLED)
> self.btnDisplay.grid(row=3, column=2, padx=5, pady=5)
>
> self.btnDisplay = Button(self, text="/", state=DISABLED)
> self.btnDisplay.grid(row=3, column=3, padx=5, pady=5)
>
> #def Display(self):
>
>
> if __name__ == "__main__":
> guiFrame = GUIFramework()
> guiFrame.mainloop()
>
>
>
>
>
>
>
> #! /usr/bin/env python
> from Tkinter import *
> import tkMessageBox
>
> class GUIFramework(Frame):
> """This is the GUI"""
>
> def __init__(self,master=None):
> """Initialize yourself"""
>
> """Initialise the base class"""
> Frame.__init__(self,master)
>
> """Set the Window Title"""
> self.master.title("Calculator")
>
> """Display the main window"
> with a little bit of padding"""
> self.grid(padx=10,pady=10)
> self.CreateWidgets()
>
> def CreateWidgets(self):
>
> #self.pack_propagate(0)
>
> self.enText = Entry(self)
> self.enText.grid(row=0, column=0, columnspan=8, padx=5, pady=5)
>
> self.enText = Entry(self)
> self.enText.grid(row=1, column=0, columnspan=8, padx=5, pady=5)
>
>
> self.btnDisplay = Button(self, text="1", command=Display('1'))
> self.btnDisplay.grid(row=3, column=0, padx=5, pady=5)
>
> self.btnDisplay = Button(self, text="2", state=DISABLED)
> self.btnDisplay.grid(row=3, column=1, padx=5, pady=5)
>
> self.btnDisplay = Button(self, text="3", state=DISABLED)
> self.btnDisplay.grid(row=3, column=2, padx=5, pady=5)
>
> self.btnDisplay = Button(self, text="+", state=DISABLED)
> self.btnDisplay.grid(row=3, column=3, padx=5, pady=5)
>
> self.btnDisplay = Button(self, text="4", state=DISABLED)
> self.btnDisplay.grid(row=4, column=0, padx=5, pady=5)
>
> self.btnDisplay = Button(self, text="5", state=DISABLED)
> self.btnDisplay.grid(row=4, column=1, padx=5, pady=5)
>
> self.btnDisplay = Button(self, text="6", state=DISABLED)
> self.btnDisplay.grid(row=4, column=2, padx=5, pady=5)
>
> self.btnDisplay = Button(self, text="-", state=DISABLED)
> self.btnDisplay.grid(row=4, column=3, padx=5, pady=5)
>
> self.btnDisplay = Button(self, text="7", state=DISABLED)
> self.btnDisplay.grid(row=5, column=0, padx=5, pady=5)
>
> self.btnDisplay = Button(self, text="8", state=DISABLED)
> self.btnDisplay.grid(row=5, column=1, padx=5, pady=5)
>
> self.btnDisplay = Button(self, text="9", state=DISABLED)
> self.btnDisplay.grid(row=5, column=2, padx=5, pady=5)
>
> self.btnDisplay = Button(self, text="*", state=DISABLED)
> self.btnDisplay.grid(row=5, column=3, padx=5, pady=5)
>
> self.btnDisplay = Button(self, text="0", state=DISABLED)
> self.btnDisplay.grid(row=6, column=0, padx=5, pady=5)
>
> self.btnDisplay = Button(self, text="C", state=DISABLED)
> self.btnDisplay.grid(row=6, column=1, padx=5, pady=5)
>
> self.btnDisplay = Button(self, text="r", state=DISABLED)
> self.btnDisplay.grid(row=6, column=2, padx=5, pady=5)
>
> self.btnDisplay = Button(self, text="/", state=DISABLED)
> self.btnDisplay.grid(row=6, column=3, padx=5, pady=5)
> #self.btnDisplay.pack(fill=BOTH, expand=1)
>
> def Display(xbtn):
> if xbtn==1:
> print 1
>
> if __name__ == "__main__":
> guiFrame = GUIFramework()
> guiFrame.mainloop()
>
>
>
>
>
> command=self.Display(xbtn)
>
> Traceback (most recent call last):
> File "C:/Users/saftarn/Desktop/guiexperiments/defdonka.py", line 87, in
> <module>
> guiFrame = GUIFramework()
> File "C:/Users/saftarn/Desktop/guiexperiments/defdonka.py", line 20, in
> __init__
> self.CreateWidgets()
> File "C:/Users/saftarn/Desktop/guiexperiments/defdonka.py", line 33, in
> CreateWidgets
> self.btnDisplay = Button(self, text="1", command=self.Display('1'))
> TypeError: Display() takes exactly 1 argument (2 given)
>
>
> command=Display(xbtn)
>
> Traceback (most recent call last):
> File "C:/Users/saftarn/Desktop/guiexperiments/defdonka.py", line 87, in
> <module>
> guiFrame = GUIFramework()
> File "C:/Users/saftarn/Desktop/guiexperiments/defdonka.py", line 20, in
> __init__
> self.CreateWidgets()
> File "C:/Users/saftarn/Desktop/guiexperiments/defdonka.py", line 33, in
> CreateWidgets
> self.btnDisplay = Button(self, text="1", command=Display('1'))
> NameError: global name 'Display' is not defined
Display is a method so should have self as first argument
def Display(self, xbtn):
...
also when you assign it to the button command it needs to be
self.Display you have another problem here though, you are calling
the method as you create the button... rather than calling the
method when the button is pressed
quick example:
def number1call():
print "Number 1 pressed"
number1 = Button(parent, text = "1", width = 3, command=number1call)
number1.pack()
Note I do not call number1call when I assign it to the buttons command
calling this function is done when the button is pressed.
Cheers
Martin
--
signature file not found, must be something I ate
More information about the Tkinter-discuss
mailing list