Entry self.r输入'1,12',我试图将其转换为(1,12)的元组。方法: tup = tuple(int(x)for self in self.r.split(“,”))适用于正常的python程序,但是当与来自Entry的输入一起使用时,会给出此错误:ValueError:即使输入为int,也无法将字符串从Entry转换为int
Exception in Tkinter callback
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/tkinter/__init__.py", line 1549, in __call__
return self.func(*args)
File "/Users/LazyLinh/PycharmProjects/TimeGenGUI/GUI/__init__.py", line 54, in calculateTime
tup = tuple(int(x) for x in self.r.split(","))
File "/Users/LazyLinh/PycharmProjects/TimeGenGUI/GUI/__init__.py", line 54, in <genexpr>
tup = tuple(int(x) for x in self.r.split(","))
ValueError: invalid literal for int() with base 10: ''
问题可能是什么?这是我的完整代码:
from tkinter import *
import math
class TimeGenerator:
def __init__(self,master):
frame = Frame(master)
frame.grid()
label_iso = Label(root, text="Isotope A, Element")
label_vol = Label(root, text="Voltage")
label_range = Label(root, text="Charge Range")
entry_iso = Entry(root)
entry_vol = Entry(root)
entry_range = Entry(root)
label_iso.grid(row=0, sticky=E)
label_vol.grid(row=1, sticky=E)
label_range.grid(row=2, sticky=E)
entry_iso.grid(row=0, column=1)
entry_vol.grid(row=1, column=1)
entry_range.grid(row=2,column=1)
button = Button(root, text='Time Range', command=self.calculateTime)
button.grid(row=3, columnspan=2)
self.iso = entry_iso.get()
self.vol = entry_vol.get()
self.r = entry_range.get()
def calculateTime(self):
x = 5
self.iso.replace(" ", "")
list = []
for e in self.iso.split(","):
list.append(e)
f = open("/Users/LazyLinh/PycharmProjects/mass.mas12.txt", "r")
i = 0
while (i < 40):
header = f.readline()
i += 1
for line in f:
line = line.strip()
columns = line.split()
if (list[0] == columns[5]):
if (list[1] == columns[6]):
self.mass = float(columns[13]) + float(columns[14])
self.r.replace(" ", "")
tup = tuple(int(x) for x in self.r.split(","))
list = []
for q in range(tup[0], tup[1] + 1):
y = x * math.sqrt(self.mass/(2 * q * float(self.vol)))
list.append(y)
i = tup[0]
for time in list:
print(i, ':', time)
i = i + 1
root = Tk()
b = TimeGenerator(root)
root.mainloop()
谢谢!!!
违规前行,打印出'self.r'。它是以逗号结尾还是以两个连续的逗号开始? – L3viathan
分割将返回标记类型字符串,如果该字符串不是有效整数,则可能会导致此类错误。所以我认为你可能会得到一个令牌(分割后),这是一个空格(不是有效的字符串),这个错误。 – cafebabe1991
在输入任何内容之前,您立即获取条目的内容(在“_ _init__”中),您希望获取self.calculateTime中的值,即在输入内容后。 –