Python GUI应用程序

时间:2016-10-27 19:13:23

标签: python user-interface tkinter

我正在尝试将此计算器转换为多窗口应用程序。我的目标是拥有"文件"菜单标签在计算器和" pig"之间切换。帧。猪架将有一个我已设计的猪拉丁语转换器。另一个目标是让其他标签实际上做他们所说的有设置窗口等。

PS:第一次发帖!

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()

下面是解释器抛出的错误:

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'

1 个答案:

答案 0 :(得分:2)

问题是initcalcUI()调用forget(),然后在调用后的几行内为self.calc_frame分配值。您将遇到与self.pig_frame相同的问题。

forget()中,您可以使用以下方法测试以查看属性是否存在:

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

或者,您可以将每个来电换入try/except并忽略AttributeError