我是python的新手,不明白这个问题的其他答案。为什么当我运行代码时,int(weight[0])
不将变量“weight”转换为整数。尽你最大的努力让它哑口无言,因为我真的是新来的,还不太明白其中的大部分。这是我的代码的相关部分
weight = (lb.curselection())
print ("clicked")
int(weight[0])
print (weight)
print (type(weight))
这是我的剧本代码
lb = Listbox(win, height=240)
lb.pack()
for i in range(60,300):
lb.insert(END,(i))
def select(event):
weight = (lb.curselection())
print ("clicked")
int(weight[0])
print (weight)
print (type(weight))
lb.bind("<Double-Button-1>", select)
谢谢
当我运行代码时,它会出现TypeError: int() argument must be a string, a bytes-like object or a number, not 'tuple'
我希望它能将“weight”变量转换成一个整数,这样我就可以用它进行数学运算了。
完整回溯:Traceback (most recent call last):
File "C:\Users\Casey\AppData\Local\Programs\Python\Python36-32\lib\tkinter\__init__.py", line 1699, in __call__
return self.func(*args)
File "C:/Users/Casey/AppData/Local/Programs/Python/Python36-32/s.py", line 11, in select
int(weight)
TypeError: int() argument must be a string, a bytes-like object or a number, not 'tuple'
我注意到你在这里使用
lb.bind("<Double-Button-1>", select)
。这确实解决了curselection()
返回最后一个选定列表项的问题,但我认为使用lb.bind('<<ListboxSelect>>', select)
会更好地解决这个问题。绑定到<<ListboxSelect>>
有效,因为此事件在所选内容更改后触发,并且当您使用此事件调用curselection()
时,您将获得所需的正确输出。下面是一段代码,提供了
<<ListboxSelect>>
事件的示例用法:您将注意到控制台中的打印输出将是单击后的当前选定项。这将避免双击的需要。
你要找的是
int
是一个返回整数的函数,因此必须将该返回值赋给变量。如果您要寻找的是用第一条记录的值重新分配变量
weight
,那么该代码应该适合您。如果项已经是一个整数,那么
int
调用可能是多余的,您可以使用相关问题 更多 >
编程相关推荐