在Python中将元组转换为int

cas*_*yan 4 python tkinter python-3.x

我在python上是全新的,并且不理解这个问题的其他答案.为什么当我运行我的代码时,不会int(weight[0])将变量"weight"转换为整数.尽力使其愚蠢,因为我真的很新,但仍然不太了解它的大部分内容.这是我的代码的相关部分

weight = (lb.curselection())
    print ("clicked")
    int(weight[0])
    print (weight)
    print (type(weight))
Run Code Online (Sandbox Code Playgroud)

并且这是我脚本的代码

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)
Run Code Online (Sandbox Code Playgroud)

谢谢

当我运行代码时,它会出现,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'

Sta*_*ael 14

你在寻找什么

weight = int(weight[0])
Run Code Online (Sandbox Code Playgroud)

int是一个返回整数的函数,因此您必须将该返回值赋给变量.

如果您要查找的是将变量weight与其第一条记录的值重新分配,那么该代码应该适合您.

如果该项目已经是一个整数,那么该int调用可能是多余的,您可能只能使用它

weight = weight[0]
Run Code Online (Sandbox Code Playgroud)