我有以下代码。我无法按自己创建的任何按钮,因为我有一个错误:NameError: name 'equation' is not defined
每个按钮上都有。这有什么问题?我是Python的新手。我尝试将包含方程式的这段代码放在不同的位置(外部类,位于main()函数中),但是这些都不适合我
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()
您需要在类中包括表达式变量和所有函数,然后为所有要成为“全局”变量提供一个以“。”开头的名称self.
。像这样:
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()
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句