Python 3遭遇[解码错误-输出不是utf-8](升华文本2)

2024-06-16 10:12:38 发布

您现在位置:Python中文网/ 问答频道 /正文

我是新来的python学习者,我正试图使用标准的Tkinter库创建一个计算器。每次尝试构建时,都会遇到以下错误:

    File "C:\Python33\calc.py", line 10
    [Decode error - output not utf-8]
    [Finished in 0.1s with exit code 1]

构建系统是基于python的,还是需要在pyhton的包中更改一些内容? 提前谢谢:)

这是我的代码:

from tkinter import *

class Calculon:
    def __init__(self, master):
        # the self variable represents the instance variable of the object
        buttonText = ["1", "2", "3", "+", "4", "5", "6", "-", "7", "8", "9", "*", "0", ".", "=", "/"]
        """
        The instance variable is bind to a anonymous lamda function which doesnt need an include statement
        When a event ocurres, an handler is called
        """
        master.bind(‘<Escape>’, lambda e: master.quit())
        master.bind(‘<Return>’, lambda e: self.buttonPress(“=”))
        """
        loops trought each in buttonText array
        The following line binds each character to its own buttonPress 
        """
        for each in buttonText:
            master.bind(each, lambda e: self.buttonPress(repr(e.char).strip(“‘”)))
            """
            Declaring the result has a string value
            Creation of labels which contain the result value's
            while grid orginazes the table  

            """
            self.result = StringVar()
            self.result.set(“0″)
            self.result = Label(master, anchor=E, textvariable=self.result, justify=RIGHT, bg=”white”)
            self.result.grid(row=0, column=0, columnspan=4, sticky=E+N+S+W)

            self.operator = StringVar()
            self.operator.set(“”)
            self.operator = Label(master, anchor=W, textvariable=self.operator, justify=LEFT, bg=”grey”)
            self.operator.grid(row=0, column=0, sticky=W+N+S+E)

            self.temp = “”

            self.operations = {“+”: self.add, “-”: self.sub, “*”: self.mul, “/”:self.div}

            self.menubar = Menu(master)

            self.pullDown = Menu(self.menubar, tearoff=0)
            self.pullDown.add_separator()
            self.pullDown.add_command(label=”Quit”, command=master.quit)

            self.menubar.add_cascade(label=”Do it”, menu=self.pullDown)

            master.configure(menu=self.menubar)

            """

            Loop to limit the ammount of labels per row

            """

            rowC = 1
            colC = 0
            for item in buttonText:
                if colC > 3:
                    colC = 0
                    rowC = rowC + 1
                    Button(master, text=str(item), command=lambda i=item:self.buttonPress(i)).grid(row=rowC, column=colC)
                    colC = colC + 1        

    def add(self, a, b):
            return a+b

    def sub(self, a, b):
        return a-b

    def mul(self, a, b):
        return a*b

    def div(self, a, b):
        if b != 0:
        return a/b
        else:
        return “division by zero”

    def buttonPress(self, c):
        if ((ord(c) > 47) and (ord(c) < 58)) or (ord(c) == 46):
        if len(self.result.get()) > 10:
        return -1
        if self.result.get() != “0″:
        self.result.set(self.result.get() + c)
        else:
        self.result.set(c)
        elif (ord(c) > 41) and (ord(c) < 48) or ord(c) == 61:
        if self.operator.get() == “” or self.operator.get() == “=”:
        self.operator.set(c)
        self.temp = int(self.result.get())
        elf.result.set(“0″)
        else:
        result = self.operations[self.operator.get()](self.temp, int(self.result.get()))
        if result < 0:
        self.result.configure(fg=”red”)
        else:
        self.result.configure(fg=”black”)
        self.result.set(str(result))
        self.operator.set(c)

    def main():
        root = Tk()
        calc = Calculon(root)

        root.mainloop()
        root.destroy()

    if __name__ == “__main__”:
    main()

Tags: theinselfmasteraddgetreturnif