gpt4 book ai didi

Python 3遇到【Decode error - output not utf-8】(sublime text 2)

转载 作者:行者123 更新时间:2023-11-28 18:48:49 25 4
gpt4 key购买 nike

我是 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()

最佳答案

您正在使用 ‘’“” 来表示字符串,但这些不是正确的字符。您必须分别用 ''"" 替换它们。

参见 String literals section of the lexical analysis reference有关正确语法的更多信息。

关于Python 3遇到【Decode error - output not utf-8】(sublime text 2),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16252152/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com