David Bognar
David Bognar

Reputation: 1

Python GUI Application

I am trying to turn this calculator into a multi window application. My goal is to have the "file" menu tab switch between the calculator and the "pig" frame. The pig frame will have a pig latin language converter I've already designed. Another goal is to have the other tabs actually do what they say like have a settings window ect.

PS: First time post!

import parser
import time

from Tkinter import *
from Tkinter import Tk, W, E
from ttk import Frame, Button, Style
from ttk import Entry


class Calculator(Frame):

    def __init__(self, parent):
############## INITIAL COMMIT #####################
        Frame.__init__(self, parent)   

############## INITIAL CALL ##################### 
        self.parent = parent
        self.initcalcUI()

############## INDIVIDUAL FRAMES #####################

        self.calc_frame = Frame(self)
        self.pig_frame = Frame(self)

############## MENUS #####################
        mymenu = Menu(self.parent)
        self.parent.config(menu=mymenu)

        fileMenu = Menu(mymenu)
        fileMenu.add_command(label="My Files", command=self.initcalcUI)
        fileMenu.add_command(label="New-File", command=self.initpigUI)

        editmenu = Menu(mymenu)
        editmenu.add_command(label="Copy")
        editmenu.add_command(label="Paste")

        historymenu = Menu(mymenu)
        historymenu.add_command(label="Display History")

        settingsmenu = Menu(mymenu)
        settingsmenu.add_command(label="Settings....")


        mymenu.add_cascade(label="Files", menu=fileMenu)
        mymenu.add_cascade(label="Edit", menu=editmenu)
        mymenu.add_cascade(label="History", menu=historymenu)
        mymenu.add_cascade(label="Settings", menu=settingsmenu)

    def initpigUI(self):
        self.forget()

        self.parent.title("Pig Latin")

        self.pig_frame = Frame(self)
        self.pig_frame.pack()

        b1 = Button(self.pig_frame, text = "Hello")
        b1.pack()

    def initcalcUI(self):

        self.forget()


        self.parent.title("Calculator")

        Style().configure("TButton", padding=(0, 5, 0, 5), 
            font='serif 10')
        self.calc_frame = Frame(self)
        self.calc_frame.pack()

        self.calc_frame.columnconfigure(0, pad=3)
        self.calc_frame.columnconfigure(1, pad=3)
        self.calc_frame.columnconfigure(2, pad=3)
        self.calc_frame.columnconfigure(3, pad=3)

        self.calc_frame.rowconfigure(0, pad=3)
        self.calc_frame.rowconfigure(1, pad=3)
        self.calc_frame.rowconfigure(2, pad=3)
        self.calc_frame.rowconfigure(3, pad=3)
        self.calc_frame.rowconfigure(4, pad=3)

        self.entry = Entry(self.calc_frame)
        self.entry.grid(row=0, columnspan=4, sticky=W+E)
        cls = Button(self.calc_frame, text="Cls", command = self.clear_all)
        cls.grid(row=1, column=0)
        bck = Button(self.calc_frame, text="Back")
        bck.grid(row=1, column=1)
        lbl = Button(self.calc_frame)
        lbl.grid(row=1, column=2)    
        clo = Button(self.calc_frame, text="Close")
        clo.grid(row=1, column=3)        
        sev = Button(self.calc_frame, text="7", command =  lambda: self.insert("7"))
        sev.grid(row=2, column=0)        
        eig = Button(self.calc_frame, text="8",  command =  lambda: self.insert("8"))
        eig.grid(row=2, column=1)         
        nin = Button(self.calc_frame, text="9",  command =  lambda: self.insert("9"))
        nin.grid(row=2, column=2) 
        div = Button(self.calc_frame, text="/",  command =  lambda: self.insert("/"))
        div.grid(row=2, column=3) 

        fou = Button(self.calc_frame, text="4",  command =  lambda: self.insert("4"))
        fou.grid(row=3, column=0)        
        fiv = Button(self.calc_frame, text="5",  command =  lambda: self.insert("5"))
        fiv.grid(row=3, column=1)         
        six = Button(self.calc_frame, text="6",  command =  lambda: self.insert("6"))
        six.grid(row=3, column=2) 
        mul = Button(self.calc_frame, text="*",  command =  lambda: self.insert("*"))
        mul.grid(row=3, column=3)    

        one = Button(self.calc_frame, text="1",  command =  lambda: self.insert("1"))
        one.grid(row=4, column=0)        
        two = Button(self.calc_frame, text="2",  command =  lambda: self.insert("2"))
        two.grid(row=4, column=1)         
        thr = Button(self.calc_frame, text="3",  command =  lambda: self.insert("3"))
        thr.grid(row=4, column=2) 
        mns = Button(self.calc_frame, text="-",  command =  lambda: self.insert("-"))
        mns.grid(row=4, column=3)         

        zer = Button(self.calc_frame, text="0",  command =  lambda: self.insert("0"))
        zer.grid(row=5, column=0)        
        dot = Button(self.calc_frame, text=".",  command =  lambda: self.insert("."))
        dot.grid(row=5, column=1)         
        equ = Button(self.calc_frame, text="=", command=self.calc)
        equ.grid(row=5, column=2) 
        pls = Button(self.calc_frame, text="+",  command =  lambda: self.insert("+"))
        pls.grid(row=5, column=3)

        #self.calc_frame.grid_forget()

        self.pack()
        #self.pack_forget()

    def clear_all(self):
        self.entry.delete(0, "end")

    def insert(self, num):
        self.entry.insert("insert", num)

    def calc(self, *args):
        userinput = self.entry.get()
        userlist = []
        for i in userinput:
            userlist.append(i)
        while " " in userlist: userlist.remove(" ")

        userequation = ""

        for i in userlist:
            userequation = userequation + i

        equation = parser.expr(userequation).compile()

        self.entry.delete(0, "end")
        self.entry.insert(0, eval(equation))

    def forget(self):
        self.calc_frame.grid_remove()
        self.pig_frame.grid_remove()



root = Tk()
app = Calculator(root)
root.mainloop()

Heres the error the interpreter throws:

Traceback (most recent call last):
  File "Documents\GUI Calculator.py", line 160, in <module>
    app = Calculator(root)
  File "Documents\GUI Calculator.py", line 18, in __init__
    self.initcalcUI()
  File "Documents\GUI Calculator.py", line 61, in initcalcUI
    self.forget()
  File "Documents\GUI Calculator.py", line 157, in forget
    self.calc_frame
AttributeError: Calculator instance has no attribute 'calc_frame'

Upvotes: 0

Views: 92

Answers (1)

martineau
martineau

Reputation: 123521

The problem is initcalcUI() calls forget() before you've assigned a value to self.calc_frame a few lines after it's called. You're going to have the same problem with self.pig_frame.

In forget() you could test to see if the attributes exist by using:

def forget(self):
    if hasattr(self, 'calc_frame'):
        self.calc_frame.grid_remove()
    if hasattr(self, 'pig_frame'):
        self.pig_frame.grid_remove()

Alternatively you could wrap each call in a try/except and ignore AttributeError.

Upvotes: 2

Related Questions