我是 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'
我希望它能将“权重”变量转换为整数,这样我就可以将它用于数学运算。
完整回溯:回溯(最近调用最后):
文件“C:\Users\Casey\AppData\Local\Programs\Python\Python36-32\lib\tkinter\__init__.py”,第 1699 行,在 __call__ 中
返回 self.func(*args)
选择文件“C:/Users/Casey/AppData/Local/Programs/Python/Python36-32/s.py”,第 11 行
整数(重量)
TypeError: int() 参数必须是字符串、类字节对象或数字,而不是“元组”
最佳答案
你要找的是
weight = int(weight[0])
int
是一个返回整数的函数,因此您必须将该返回值分配给一个变量。
如果您正在寻找的是用第一条记录的值重新分配变量 weight
,那么该代码应该适合您。
如果该项目已经是一个整数,那么 int
调用可能是多余的,您也许可以通过
weight = weight[0]
关于python - 在 Python 中将元组转换为整数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45465463/