iT邦幫忙

0

在做一個計算機GUI的時候使用了eval(),但是系統卻出現錯誤,有人可以幫忙嗎?

  • 分享至 

  • xImage

程式

import tkinter as tk

OFF_WHITE = "#F8FAFF"
LIGHT_GRAY = "#F5F5F5"
LABEL_COLOR = "#25265E"
WHITE = "#FFFFFF"
LIGHT_BLUE = "#CCEDFF"
SMALL_FONT_STYLE = ("Arial",16)
LARGE_FONT_STYLE = ("Arial",40,"bold")
DIGIT_FONT_STYLE = ("Arial",24,"bold")
DEFAULT_FONT_STYLE = ("Arial",20)
class Calculator:
    # init
    def __init__(self):
        self.window = tk.Tk()
        self.window.geometry("375x667")
        self.window.resizable(0,0)
        self.window.title("Calculator")

        self.total_expression = ""
        self.current_expression = ""
        self.display_frame = self.create_display_frame()
        self.total_label,self.label = self.create_display_labels()
        self.digits ={
            7:(1,1),8:(1,2),9:(1,3),
            4:(2,1),5:(2,2),6:(2,3),
            1:(3,1),2:(3,2),3:(3,3),
            0:(4,2),".":(4,1)
        }
        self.operations = {"/": "\u00F7", "*": "\u00D7", "-": "-", "+": "+"}
        self.buttons_frame = self.create_button_frame()
        self.buttons_frame.rowconfigure(0,weight=1)

        for x in range(1,5):
            self.buttons_frame.rowconfigure(x,weight=1)
            self.buttons_frame.columnconfigure(x,weight=1)
        self.create_digit_button()
        self.create_operator_buttons()
        self.create_special_buttons()


    def create_special_buttons(self):
        self.create_clear_button()
        self.create_equals_button()


    def create_display_labels(self):
            total_label = tk.Label(self.display_frame, text= self.total_expression,anchor=tk.E,bg=LIGHT_GRAY,fg=LABEL_COLOR,padx=24,font=SMALL_FONT_STYLE)
            total_label.pack(expand=True,fill="both")

            label = tk.Label(self.display_frame, text= self.current_expression,anchor=tk.E,bg=LIGHT_GRAY,fg=LABEL_COLOR,padx=24,font=LARGE_FONT_STYLE)
            label.pack(expand=True,fill="both")

            return total_label,label


    def create_display_frame(self):
        frame = tk.Frame(self.window,height=221,bg=LIGHT_GRAY)
        frame.pack(expand=True,fill="both")
        return frame
    

    def add_to_expression(self,value):
        self.current_expression += str(value)
        self.update_labels()


    def create_operator_buttons(self):
        i = 0
        for operator,symbol in self.operations.items():
            button = tk.Button(self.buttons_frame,text=symbol,bg=OFF_WHITE,fg=LABEL_COLOR,font=DEFAULT_FONT_STYLE,borderwidth=0,command=lambda x=operator:self.append_operator(x))
            button.grid(row=i,column=4,sticky=tk.NSEW)
            i += 1


    def clear(self):
        self.current_expression = ""
        self.total_expression = ""
        self.update_total_labels()
        self.update_labels()


    def create_clear_button(self):
            button = tk.Button(self.buttons_frame,text="C",bg=OFF_WHITE,fg=LABEL_COLOR,font=DEFAULT_FONT_STYLE,borderwidth=0,command=self.clear())
            button.grid(row=0,column=1,columnspan=3,sticky=tk.NSEW)


    def evaluate(self):
        self.total_expression += self.current_expression
        self.update_total_labels()
        self.current_expression = str(eval(str.total_expression))
        self.total_expression = ""
        self.update_labels()


    def create_equals_button(self):
            button = tk.Button(self.buttons_frame,text="=",bg=LIGHT_BLUE,fg=LABEL_COLOR,font=DEFAULT_FONT_STYLE,borderwidth=0,command=self.evaluate())
            button.grid(row=4,column=3,columnspan=2,sticky=tk.NSEW)           


    def create_digit_button(self):
        for digit,grid_value in self.digits.items():
            button = tk.Button(self.buttons_frame,text=str(digit),bg=WHITE,fg=LABEL_COLOR,font=DIGIT_FONT_STYLE,borderwidth=0,command=lambda x=digit: self.add_to_expression(x))
            button.grid(row=grid_value[0],column=grid_value[1],sticky=tk.NSEW)


    def append_operator(self,operator):
        self.current_expression += operator
        self.total_expression += self.current_expression
        self.current_expression = ""
        self.update_total_labels()
        self.update_labels()


    def create_button_frame(self):
        frame = tk.Frame(self.window)
        frame.pack(expand=True,fill="both")
        return frame


    def update_total_labels(self):
        self.total_label.config(text=self.total_expression)


    def update_labels(self):
        self.label.config(text=self.current_expression)


    def run(self):
        self.window.mainloop()



if __name__ == "__main__":
    calc = Calculator()
    calc.run()

錯誤
`Traceback (most recent call last):
File "c:\Users\user\OneDrive\桌面\new_code\calculator\calculator.py", line 135, in
calc = Calculator()
File "c:\Users\user\OneDrive\桌面\new_code\calculator\calculator.py", line 39, in init
self.create_special_buttons()
File "c:\Users\user\OneDrive\桌面\new_code\calculator\calculator.py", line 44, in create_special_buttons
self.create_equals_button()
File "c:\Users\user\OneDrive\桌面\new_code\calculator\calculator.py", line 97, in create_equals_button
button = tk.Button(self.buttons_frame,text="=",bg=LIGHT_BLUE,fg=LABEL_COLOR,font=DEFAULT_FONT_STYLE,borderwidth=0,command=self.evaluate())
File "c:\Users\user\OneDrive\桌面\new_code\calculator\calculator.py", line 91, in evaluate
self.current_expression = str(eval(str(self.total_expression)))
File "", line 0

SyntaxError: unexpected EOF while parsing`
使用venv

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

0
海綿寶寶
iT邦大神 1 級 ‧ 2022-06-18 23:47:08
最佳解答

錯誤原因是
第一次執行時
self.current_expression = str(eval(self.total_expression))裡的 total_expression 是空字串以致 eval 失敗

我要發表回答

立即登入回答