實際上我正在 tkinter 中開發一個名為“Calculator”的專案,所以我決定制作三個按鈕,上面寫著:
- 基本計算器
- 單位換算
- 金融轉換器
所以首先我制作了單位轉換器的邏輯,我制作了它并將其與主代碼成功合并而沒有出錯。但是在基本計算器的情況下,它顯示錯誤,實際上當我獨立運行它時它作業正常但是當我嘗試將它與主代碼合并時它顯示錯誤,所以請幫助我解決這個錯誤。
基本計算器程式
from tkinter import *
calculator = Tk()
calculator.geometry("436x382")
calculator.resizable(0, 0)
calculator.title("Calculator")
# continuously updates the input field whenever you enter a number
def btn_click(item):
global expression
expression = expression str(item)
input_text.set(expression)
def bt_clear():
global expression
expression = ""
input_text.set("")
def bt_equal():
global expression
result = str(eval(expression)) # 'eval':This function is used to evaluates the string expression directly
input_text.set(result)
expression = ""
expression = ""
# 'StringVar()' :It is used to get the instance of input field
input_text = StringVar()
input_frame = Frame(calculator, width=312, height=50, bd=0, highlightbackground="black", highlightcolor="black", highlightthickness=2)
input_frame.pack(side=TOP)
input_field = Entry(input_frame, font=('arial', 18, 'bold'), textvariable=input_text, width=50, bg="#055", bd=0, justify=RIGHT)
input_field.grid(row=0, column=0)
input_field.pack(ipady=10) # 'ipady' is internal padding to increase the height of input field
btns_frame = Frame(calculator, width=312, height=272.5, bg="grey")
btns_frame.pack()
clear = Button(btns_frame, text = "AC", fg = "black", width = 32, height = 3, bd = 0, padx=30, bg = "#eee", cursor = "hand2", command = lambda: bt_clear()).grid(row = 0, column = 0, columnspan = 3, padx = 1, pady = 1)
divide = Button(btns_frame, text = "/", fg = "black", width = 10, height = 3, bd = 0, bg = "#eee", cursor = "hand2", command = lambda: btn_click("/")).grid(row = 0, column = 3, padx = 1, pady = 1)
seven = Button(btns_frame, text = "7", fg = "black", width = 10, height = 3, bd = 0, bg = "#fff", cursor = "hand2", command = lambda: btn_click(7)).grid(row = 1, column = 0, padx = 1, pady = 1)
eight = Button(btns_frame, text = "8", fg = "black", width = 10, height = 3, bd = 0, bg = "#fff", cursor = "hand2", command = lambda: btn_click(8)).grid(row = 1, column = 1, padx = 1, pady = 1)
nine = Button(btns_frame, text = "9", fg = "black", width = 10, height = 3, bd = 0, bg = "#fff", cursor = "hand2", command = lambda: btn_click(9)).grid(row = 1, column = 2, padx = 1, pady = 1)
multiply = Button(btns_frame, text = "*", fg = "black", width = 10, height = 3, bd = 0, bg = "#eee", cursor = "hand2", command = lambda: btn_click("*")).grid(row = 1, column = 3, padx = 1, pady = 1)
four = Button(btns_frame, text = "4", fg = "black", width = 10, height = 3, bd = 0, bg = "#fff", cursor = "hand2", command = lambda: btn_click(4)).grid(row = 2, column = 0, padx = 1, pady = 1)
five = Button(btns_frame, text = "5", fg = "black", width = 10, height = 3, bd = 0, bg = "#fff", cursor = "hand2", command = lambda: btn_click(5)).grid(row = 2, column = 1, padx = 1, pady = 1)
six = Button(btns_frame, text = "6", fg = "black", width = 10, height = 3, bd = 0, bg = "#fff", cursor = "hand2", command = lambda: btn_click(6)).grid(row = 2, column = 2, padx = 1, pady = 1)
minus = Button(btns_frame, text = "-", fg = "black", width = 10, height = 3, bd = 0, bg = "#eee", cursor = "hand2", command = lambda: btn_click("-")).grid(row = 2, column = 3, padx = 1, pady = 1)
one = Button(btns_frame, text = "1", fg = "black", width = 10, height = 3, bd = 0, bg = "#fff", cursor = "hand2", command = lambda: btn_click(1)).grid(row = 3, column = 0, padx = 1, pady = 1)
two = Button(btns_frame, text = "2", fg = "black", width = 10, height = 3, bd = 0, bg = "#fff", cursor = "hand2", command = lambda: btn_click(2)).grid(row = 3, column = 1, padx = 1, pady = 1)
three = Button(btns_frame, text = "3", fg = "black", width = 10, height = 3, bd = 0, bg = "#fff", cursor = "hand2", command = lambda: btn_click(3)).grid(row = 3, column = 2, padx = 1, pady = 1)
plus = Button(btns_frame, text = " ", fg = "black", width = 10, height = 3, bd = 0, bg = "#eee", cursor = "hand2", command = lambda: btn_click(" ")).grid(row = 3, column = 3, padx = 1, pady = 1)
zero = Button(btns_frame, text = "0", fg = "black", width = 21, height = 3, padx=20, bd = 0, bg = "#fff", cursor = "hand2", command = lambda: btn_click(0)).grid(row = 4, column = 0, columnspan = 2, padx = 1, pady = 1)
point = Button(btns_frame, text = ".", fg = "black", width = 10, height = 3, bd = 0, bg = "#eee", cursor = "hand2", command = lambda: btn_click(".")).grid(row = 4, column = 2, padx = 1, pady = 1)
equals = Button(btns_frame, text = "=", fg = "black", width = 10, height = 3, bd = 0, bg = "#eee", cursor = "hand2", command = lambda: bt_equal()).grid(row = 4, column = 3, padx = 1, pady = 1)
calculator.mainloop()
主程式鏈接github鏈接 code.py是主程式
當我將基本計算器程式與主代碼合并時出現 錯誤 錯誤訊息
uj5u.com熱心網友回復:
從帖子中我了解到您的問題是當您單擊計算器中的數字時出現名稱錯誤。(如果我錯了,請糾正我)
我查看了您的代碼,實際上您需要做的就是expression = ""在代碼的開頭復制并定義它。
之后,不會出現錯誤。
希望能幫助到你!下次,請記住只給出最小的可重現示例。
uj5u.com熱心網友回復:
由于您已將發布的代碼放在basiccalculator()實際代碼中的函式 ( ) 中,因此expression該函式中是區域變數。所以根本沒有全域變數expression。
當你想expression在嵌套函式內部使用時,它無法找到,expression因為它被宣告為全域。
更改global expression到nonlocal expression這些嵌套函式內部將解決此問題:
def basiccalculator():
calculator = Tk()
calculator.geometry("436x382")
calculator.resizable(0, 0)
calculator.title("Calculator")
expression = ""
# continuously updates the input field whenever you enter a number
def btn_click(item):
nonlocal expression # use nonlocal instead of global
expression = expression str(item)
input_text.set(expression)
def bt_clear():
nonlocal expression
expression = ""
input_text.set("")
def bt_equal():
nonlocal expression
result = str(eval(expression)) # 'eval':This function is used to evaluates the string expression directly
input_text.set(result)
expression = ""
...
請參閱在Python的檔案global和nonlocal詳細資訊。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/371967.html
