Tkinter, a lot of buttons, make prog shorter?

skanemupp at yahoo.se skanemupp at yahoo.se
Sun Apr 6 22:50:16 EDT 2008


should i not use self. in Clean() and Calculate() either?

anyway i had changed it before. must have copied the wrong program.



#! /usr/bin/env python
from Tkinter import *
import tkMessageBox

class GUIFramework(Frame):
    """This is the GUI"""

    def __init__(self, master=None):
        """Initialize yourself"""

        self.expr = ""

        """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):

        """Create the Button, set the text and the
        command that will be called when the button is clicked"""
        btnDisplay = Button(self, text="calculate!",
command=self.Calculate)
        btnDisplay.grid(row=0, column=31)

##        """Create the Button, set the text and the
##        command that will be called when the button is clicked"""
##        self.lbText = Label(self, text="Results:")
##        self.lbText.grid(row=1, column=0)


        btnDisplay = Button(self,text='1',command=lambda
n="1":self.Display(n),width=2,height=2)
        btnDisplay.grid(row=3, column=0)

        btnDisplay = Button(self,text='2',command=lambda
n="2":self.Display(n),width=2,height=2)
        btnDisplay.grid(row=3, column=1)

        btnDisplay = Button(self,text='3',command=lambda
n="3":self.Display(n),width=2,height=2)
        btnDisplay.grid(row=3, column=2)

        btnDisplay = Button(self,text='+',command=lambda
n="+":self.Display(n),width=2,height=2)
        btnDisplay.grid(row=3, column=3)

        btnDisplay = Button(self,text='4',command=lambda
n="4":self.Display(n),width=2,height=2)
        btnDisplay.grid(row=4, column=0)

        btnDisplay = Button(self,text='5',command=lambda
n="5":self.Display(n),width=2,height=2)
        btnDisplay.grid(row=4, column=1)

        btnDisplay = Button(self,text='6',command=lambda
n="6":self.Display(n),width=2,height=2)
        btnDisplay.grid(row=4, column=2)

        btnDisplay = Button(self,text='-',command=lambda
n="-":self.Display(n),width=2,height=2)
        btnDisplay.grid(row=4, column=3)

        btnDisplay = Button(self,text='7',command=lambda
n="7":self.Display(n),width=2,height=2)
        btnDisplay.grid(row=5, column=0)

        btnDisplay = Button(self,text='8',command=lambda
n="8":self.Display(n),width=2,height=2)
        btnDisplay.grid(row=5, column=1)

        btnDisplay = Button(self,text='9',command=lambda
n="9":self.Display(n),width=2,height=2)
        btnDisplay.grid(row=5, column=2)

        btnDisplay = Button(self,text='*',command=lambda
n="*":self.Display(n),width=2,height=2)
        btnDisplay.grid(row=5, column=3)

        btnDisplay = Button(self,text='0',command=lambda
n="0":self.Display(n),width=2,height=2)
        btnDisplay.grid(row=6, column=0)

        btnDisplay =
Button(self,text='C',command=self.Clean,width=2,height=2)
        btnDisplay.grid(row=6, column=1)

        btnDisplay = Button(self,text='^.5',command=lambda n="**.
5":self.Display(n),width=2,height=2)
        btnDisplay.grid(row=6, column=2)

        btnDisplay = Button(self,text='/',command=lambda
n="/":self.Display(n),width=2,height=2)
        btnDisplay.grid(row=6, column=3)




    def Display(self, number):
        self.expr = self.expr + number
        self.lbText = Label(self, text=self.expr)
        self.lbText.grid(row=0, column=0)

    def Calculate(self):
        self.expr = str(eval(self.expr))#try catch tex 3+6+
        result = self.expr
        self.Clean()
        self.lbText = Label(self, text=result)
        self.lbText.grid(row=0, column=0)
        self.expr = ""

    def Clean(self):
        self.expr = "                                         "
        self.lbText.config(text=self.expr)
        self.lbText = Label(self, text=self.expr)
        self.lbText.grid(row=0, column=0)
        self.expr = ""
        self.lbText.config(text=self.expr)
        self.lbText = Label(self, text=self.expr)
        self.lbText.grid(row=0, column=0)



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



More information about the Python-list mailing list