此错误不断弹出我的代码中的左,右和中心。它真的很烦人。到目前为止,我已经修好了所有这些,但我似乎无法解决这个问题。
Traceback (most recent call last):
File "C:\Users\Home\Desktop\da.py", line 31, in <module>
if (number > 1):
TypeError: '>' not supported between instances of 'str' and 'int'
代码本身:
from tkinter import *
from tkinter import ttk
import tkinter as tk
def add_text():
global number
number = num_textbox.get()
print(number)
root = Tk()
root.title("Number Cent Divider")
root.geometry("330x85")
num_col_mat = Label(root, text="Your number:")
num_col_mat.pack()
num_textbox = Entry(root, bd=1)
num_textbox.pack()
enter_button = Button(root, text="Enter", command=add_text)
enter_button.pack()
root.mainloop()
if (number[-1] == 5 or number[-1] == 0):
number / 5
int(number)
if (number > 1):
answer = "\number 5 cent coins"
str(number)
popup()
else:
answer = "\number 5 cent coin"
str(number)
popup()
else:
int(number)
if (number > 1):
answer = "\number 1 cent coins"
str(number)
popup()
else:
answer = "\number 1 cent coin"
str(number)
popup()
def popup():
popup = tk.Tk()
popup.wm_title("answer")
answer = Label(popup, text=answer)
answer.pack
B1 = ttk.Button(popup, text="Ok", command=popup.destroy)
B1.pack()
任何帮助都会受到赞赏,因为这个错误不想修复。
答案 0 :(得分:1)
number
是str
,因此您首先需要将其转换为数字。 E.g:
if int(number) > 1
单独在一行上写int(number)
并不做任何事情......它只返回一个数字,然后被忽略。如果要将结果存储在number = int(number)
变量中,可以使用number
。
考虑使用number = int(num_textbox.get())
预先获得转换。 (但请注意,如果该文本框的内容不是有效数字,您将获得例外。)