TKinter, buttonwidget response problem(1) and all btns the same size(2)!

skanemupp at yahoo.se skanemupp at yahoo.se
Sat Apr 5 11:26:33 EDT 2008


On 5 Apr, 17:09, Fredrik Lundh <fred... at pythonware.com> wrote:
> skanem... at yahoo.se wrote:
> >>     def __init__(self):
> >>         # ...
> >>         button = Button(self,
> >>                         text='1',
> >>                         command=lambda n=1: self.display(n))
> >>         # ...
>
> >>     def display(self, number):
> >>         print number
>
> > should this really be inside the __init__ function?
>
> what's wrong with creating widgets in an __init__ method?
>
> </F>


i dont know, i used a piece of code i found which had a createwidgets-
method. isnt init a function, not a method btw(or it is the same
thing?)




this is the current code, only included 2 buttons here to make it
shorter. i might still have misinterpreted though, is the code
correct(it runs correct at least...)?
whats the advantage/disadvante to have it inside the init?


#! /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',command=lambda
n=1:self.Display(n))
        self.btnDisplay.grid(row=3, column=0, padx=5, pady=5)

        self.btnDisplay = Button(self,text='/',command=lambda
n="/":self.Display(n))
        self.btnDisplay.grid(row=6, column=3, padx=5, pady=5)

    def Display(self, number):
        print number

if __name__ == "__main__":
    guiFrame = GUIFramework()
    guiFrame.mainloop()






More information about the Python-list mailing list