首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Tkinter : NameError:未定义名称'equation‘

Tkinter : NameError:未定义名称'equation‘
EN

Stack Overflow用户
提问于 2020-12-30 03:07:26
回答 1查看 77关注 0票数 1

我有以下代码。我不能按下我创建的任何按钮,因为我得到了一个错误:每个按钮上都有NameError: name 'equation' is not defined。这有什么问题呢?我是Python的新手。我曾尝试将包含公式的这段代码放在不同的位置(在类外部,在main() func中),但对我来说都不起作用

代码语言:javascript
复制
from tkinter import * 

expression = ""


class Calc(Frame):
    
    def __init__(self):
        super().__init__()

        self.UI()

    def UI(self):

        equation = StringVar()

        equation.set("0")

        large_font = ('Arial', 30)

        self.master.title("Kalkulator")
        self.pack(fill = BOTH, expand = 1)

        calc_txt = Entry(width = 204, font = large_font, bg = "gray", fg = "white", textvariable = equation)
        calc_txt.place(x = 0 , y = 0)

        percentage_btn = Button(width = 6, height = 2, text = "%", bg = "orange", fg = "white", borderwidth=2, relief="solid", command = lambda: press("%"))
        percentage_btn.place(x = 0, y = 49)

        sqr_btn = Button(width = 6, height = 2, text = "^", bg = "orange", fg = "white", borderwidth=2, relief="solid", command = lambda: press("**"))
        sqr_btn.place(x = 51, y = 49)

        sqr_root_btn = Button(width = 6, height = 2, text = "", bg = "orange", fg = "white", borderwidth=2, relief="solid", command = lambda: press(3))
        sqr_root_btn.place(x = 102, y = 49)

        multiply_btn = Button(width = 6, height = 2, text = "*", bg = "orange", fg = "white", borderwidth=2, relief="solid", command = lambda: press("*"))
        multiply_btn.place(x = 153, y = 49)

        divide_btn = Button(width = 6, height = 2, text = "/", bg = "orange", fg = "white", borderwidth=2, relief="solid", command = lambda: press("/"))
        divide_btn.place(x = 153, y = 89)

        add_btn = Button(width = 6, height = 2, text = "+", bg = "orange", fg = "white", borderwidth=2, relief="solid", command = lambda: press("+"))
        add_btn.place(x = 153, y = 129)

        substrct_btn = Button(width = 6, height = 2,  text = "-", bg = "orange", fg = "white", borderwidth=2, relief="solid", command = lambda: press("-"))
        substrct_btn.place(x = 153, y = 169)

        equal_btn = Button(width = 6, height = 2, text = "=", bg = "orange", fg = "white", borderwidth=2, relief="solid", command = equalpress)
        equal_btn.place(x = 153, y = 209)

        clear_btn = Button(width = 6, height = 2, text = "C", bg = "red", fg = "white", borderwidth=2, relief="solid", command = clear)
        clear_btn.place(x = 0, y = 209)

        dot_btn = Button(width = 6, height = 2,  text = ".", bg = "white", fg = "black", borderwidth=2, relief="solid", command = lambda: press("."))
        dot_btn.place(x = 51, y = 209)

        zero_btn = Button(width = 6, height = 2, text = "0", bg = "white", fg = "black", borderwidth=2, relief="solid", command = lambda: press(0))
        zero_btn.place(x = 102, y = 209)

        one_btn = Button(width = 6, height = 2, text = "1", bg = "white", fg = "black", borderwidth=2, relief="solid", command = lambda: press(1))
        one_btn.place(x = 0, y = 169)

        two_btn = Button(width = 6, height = 2, text = "2", bg = "white", fg = "black", borderwidth=2, relief="solid", command = lambda: press(2))
        two_btn.place(x = 51, y = 169)

        three_btn = Button(width = 6, height = 2, text = "3", bg = "white", fg = "black", borderwidth=2, relief="solid", command = lambda: press(3))
        three_btn.place(x = 102, y = 169)

        four_btn = Button(width = 6, height = 2, text = "4", bg = "white", fg = "black", borderwidth=2, relief="solid", command = lambda: press(4))
        four_btn.place(x = 0, y = 129)

        five_btn = Button(width = 6, height = 2, text = "5", bg = "white", fg = "black", borderwidth=2, relief="solid", command = lambda: press(5))
        five_btn.place(x = 51, y = 129)

        six_btn = Button(width = 6, height = 2, text = "6", bg = "white", fg = "black", borderwidth=2, relief="solid", command = lambda: press(6))
        six_btn.place(x = 102, y = 129)

        seven_btn = Button(width = 6, height = 2, text = "7", bg = "white", fg = "black", borderwidth=2, relief="solid", command = lambda: press(7))
        seven_btn.place(x = 0, y = 89)

        eight_btn = Button(width = 6, height = 2, text = "8", bg = "white", fg = "black", borderwidth=2, relief="solid", command = lambda: press(8))
        eight_btn.place(x = 51, y = 89)

        nine_btn = Button(width = 6, height = 2, text = "9", bg = "white", fg = "black", borderwidth=2, relief="solid", command = lambda: press(9))
        nine_btn.place(x = 102, y = 89)


def press(num): 
    
    global expression 
  
    expression = expression + str(num) 

    equation.set(expression)


def equalpress(): 
    
    try:

        global expression

        total = str(eval(expression))

        equation.set(total)

        expression = ""

    except:

        equation.set(" error ")
        expression = ""


def clear(): 
    global expression 
    expression = "" 
    equation.set("") 


def main():

    window = Tk()
    window.resizable(0,0)
    window.geometry("204x250")
    app = Calc()
    window.mainloop()


if __name__ == '__main__':
    main()
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-12-30 03:13:16

你需要在你的类中包含表达式变量和你的所有函数,然后给你想要“全局”的所有变量一个以self.开头的名称。如下所示:

代码语言:javascript
复制
from tkinter import *

class Calc(Frame):
    def __init__(self):
        super().__init__()
        self.UI()

    def UI(self):
        self.equation = StringVar()
        self.equation.set("0")
        self.expression = ''
        large_font = ('Arial', 30)
        calc_txt = Entry(width = 204, font = large_font, bg = "gray", fg = "white", textvariable = self.equation)
        calc_txt.place(x = 0 , y = 0)

        percentage_btn = Button(width = 6, height = 2, text = "%", bg = "orange", fg = "white", borderwidth=2, relief="solid", command = lambda: self.press("%"))
        percentage_btn.place(x = 0, y = 49)

    def press(self, num):
        self.expression = self.expression + str(num)
        self.equation.set(self.expression)

def main():
    window = Tk()
    window.resizable(0,0)
    window.geometry("204x250")
    app = Calc()
    window.mainloop()
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65497415

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档