3

我是新学习 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()
4

2 回答 2

1
于 2013-04-27T13:26:00.233 回答
0

打开Python.sublime-build,添加一行“encoding”:“cp936”,保存文件。

在以下位置找到 Python.sublime-build:</p>

Sublime Text 2 : SublimeText2\Data\Packages\Python\Python.sublime-build
Sublime Text 3 :SublimeText3\Packages\Python.sublime-package 
于 2020-05-16T09:25:26.700 回答