ValueError: invalid literal for int() with base 10: 'm:'
ValueError: invalid literal for int() with base 10: 'm:'
我在 else 语句中遇到错误,但在 if 语句中却没有,它们应该是相同的。我无法指出为什么会出现错误。
这里是错误:
line 27, in <module>
Y.append(int(i))
ValueError: invalid literal for int() with base 10: 'm:'
这是代码,我还没有完成,我还在检查错误:
from tkinter import *
root = Tk()
root.title("Y=mX+c Graph")
def startfunc():
string =''
for x in Y:
string = string+ str(x) +'\n'
my_label.config(text=string)
X=[]
Y=[]
for x in range(2):
e = Entry(root)
e.grid(row=0, column=x, padx=5, pady=10)
if(x==1):
e.insert(0,"X:")
i = e.get()
i.lstrip(":")
X.append(int(i))
else:
e.insert(0,"m:")
i = e.get()
i.lstrip(":")
Y.append(int(i))
button = Button(root, text="Start", command=startfunc)
button.grid(row=0,column =3,padx=5,pady=10)
my_label = Label(root,text='')
root.mainloop()
lstrip(':')
仅当 :
在字符串的开头但您将 :
作为第二个字符时才删除它。也许您需要 split(":")
来创建两个字符串 - 在 :
之前和 :
之后。或者,也许您应该将其切片 i = i[2:]
以从开头删除两个字符。
但是还有其他问题。 Entry
不像 input()
那样工作 - 它不等待您的数据。它只通知 tkinter
它必须在 window 中显示的内容。如果您在 Entry
之后直接使用 get()
,那么您将得到空字符串或仅得到 "m:"
。你必须在 startfunc
中使用 .get()
最小工作示例。
我把 X:
、m:
作为 Labels
放在 Entry
上面,所以我不必从 Entry
得到的值中删除它.
对于两个 Entries
,在没有 for
循环的情况下创建它们会更简单。而且代码更具可读性。
但是如果你有更多的 Entries
那么我会使用 for
循环与 list
或 tuple
而不是 range()
for number, text in enumerate(["X:", "m:"]):
完整代码:
import tkinter as tk # PEP8: `import *` is not preferred
# -- functions ---
def startfunc():
new_x = entry_x.get()
new_m = entry_m.get()
X.append(int(new_x))
Y.append(int(new_m))
string = "\n".join([str(i) for i in X])
label_x.config(text=string)
string = "\n".join([str(i) for i in Y])
label_m.config(text=string)
# --- main ---
X = [] # PEP8: spaces around `=`
Y = []
root = tk.Tk()
root.title("Y=mX+c Graph")
# ---
l = tk.Label(root, text="X:")
l.grid(row=0, column=0, padx=5, pady=10)
entry_x = tk.Entry(root)
entry_x.grid(row=1, column=0, padx=5, pady=10)
l = tk.Label(root, text="m:")
l.grid(row=0, column=1, padx=5, pady=10)
entry_m = tk.Entry(root)
entry_m.grid(row=1, column=1, padx=5, pady=10)
# ---
button = tk.Button(root, text="Start", command=startfunc)
button.grid(row=1, column=3, padx=5, pady=10)
# ---
label_x = tk.Label(root)
label_x.grid(row=2, column=0, padx=5, pady=10)
label_m = tk.Label(root)
label_m.grid(row=2, column=1, padx=5, pady=10)
# ---
root.mainloop()
我在 else 语句中遇到错误,但在 if 语句中却没有,它们应该是相同的。我无法指出为什么会出现错误。
这里是错误:
line 27, in <module>
Y.append(int(i))
ValueError: invalid literal for int() with base 10: 'm:'
这是代码,我还没有完成,我还在检查错误:
from tkinter import *
root = Tk()
root.title("Y=mX+c Graph")
def startfunc():
string =''
for x in Y:
string = string+ str(x) +'\n'
my_label.config(text=string)
X=[]
Y=[]
for x in range(2):
e = Entry(root)
e.grid(row=0, column=x, padx=5, pady=10)
if(x==1):
e.insert(0,"X:")
i = e.get()
i.lstrip(":")
X.append(int(i))
else:
e.insert(0,"m:")
i = e.get()
i.lstrip(":")
Y.append(int(i))
button = Button(root, text="Start", command=startfunc)
button.grid(row=0,column =3,padx=5,pady=10)
my_label = Label(root,text='')
root.mainloop()
lstrip(':')
仅当 :
在字符串的开头但您将 :
作为第二个字符时才删除它。也许您需要 split(":")
来创建两个字符串 - 在 :
之前和 :
之后。或者,也许您应该将其切片 i = i[2:]
以从开头删除两个字符。
但是还有其他问题。 Entry
不像 input()
那样工作 - 它不等待您的数据。它只通知 tkinter
它必须在 window 中显示的内容。如果您在 Entry
之后直接使用 get()
,那么您将得到空字符串或仅得到 "m:"
。你必须在 startfunc
.get()
最小工作示例。
我把 X:
、m:
作为 Labels
放在 Entry
上面,所以我不必从 Entry
得到的值中删除它.
对于两个 Entries
,在没有 for
循环的情况下创建它们会更简单。而且代码更具可读性。
但是如果你有更多的 Entries
那么我会使用 for
循环与 list
或 tuple
而不是 range()
for number, text in enumerate(["X:", "m:"]):
完整代码:
import tkinter as tk # PEP8: `import *` is not preferred
# -- functions ---
def startfunc():
new_x = entry_x.get()
new_m = entry_m.get()
X.append(int(new_x))
Y.append(int(new_m))
string = "\n".join([str(i) for i in X])
label_x.config(text=string)
string = "\n".join([str(i) for i in Y])
label_m.config(text=string)
# --- main ---
X = [] # PEP8: spaces around `=`
Y = []
root = tk.Tk()
root.title("Y=mX+c Graph")
# ---
l = tk.Label(root, text="X:")
l.grid(row=0, column=0, padx=5, pady=10)
entry_x = tk.Entry(root)
entry_x.grid(row=1, column=0, padx=5, pady=10)
l = tk.Label(root, text="m:")
l.grid(row=0, column=1, padx=5, pady=10)
entry_m = tk.Entry(root)
entry_m.grid(row=1, column=1, padx=5, pady=10)
# ---
button = tk.Button(root, text="Start", command=startfunc)
button.grid(row=1, column=3, padx=5, pady=10)
# ---
label_x = tk.Label(root)
label_x.grid(row=2, column=0, padx=5, pady=10)
label_m = tk.Label(root)
label_m.grid(row=2, column=1, padx=5, pady=10)
# ---
root.mainloop()