在 Python 中将元组转换为 int
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45465463/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Convert tuple to int in Python
提问by casey ryan
I'm brand new at python, and didn't understand the other answers for this question. Why when I run my code, does int(weight[0])
not convert variable "weight" into a integer. Try your best to dumb it down because I'm really new and still don't quite understand most of it. Here is the relevant section of my code
我是 python 的新手,不明白这个问题的其他答案。为什么当我运行我的代码时,不会int(weight[0])
将变量“权重”转换为整数。尽量把它变笨,因为我真的很新,仍然不太明白其中的大部分内容。这是我的代码的相关部分
weight = (lb.curselection())
print ("clicked")
int(weight[0])
print (weight)
print (type(weight))
and heres my code for this script
这是我的这个脚本的代码
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)
Thanks
谢谢
When I run the code, it comes up with TypeError: int() argument must be a string, a bytes-like object or a number, not 'tuple'
and I want it instead to convert the "weight" variable into a integer, so I can use it for math operations.
当我运行代码时,它出现了,TypeError: int() argument must be a string, a bytes-like object or a number, not 'tuple'
我希望它将“权重”变量转换为整数,以便我可以将其用于数学运算。
Full Traceback: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'
完整追溯: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'
回答by Stael
what you're looking for is
你要找的是
weight = int(weight[0])
int
is a function that returnsan integer, so you have to assign that return to a variable.
int
是一个返回整数的函数,因此您必须将该返回值分配给一个变量。
if what you're looking for is to reassign the variable weight
with the value of its first record, that code should work for you.
如果您正在寻找的是weight
使用其第一条记录的值重新分配变量,那么该代码应该适合您。
If the item is already an integer then the int
call might be redundant, you might be able to get it with just
如果该项目已经是一个整数,那么该int
调用可能是多余的,您可以只用
weight = weight[0]
回答by Mike - SMT
I noticed you were using lb.bind("<Double-Button-1>", select)
here. This does get around the issue with curselection()
returning the last selected list item but I would say using lb.bind('<<ListboxSelect>>', select)
would work better for this. Binding to <<ListboxSelect>>
works because this event triggers after the selection has changed and when you go to call curselection()
using this event instead you will get the correct output you are looking for.
我注意到你在lb.bind("<Double-Button-1>", select)
这里使用。这确实解决了curselection()
返回最后选择的列表项的问题,但我会说使用lb.bind('<<ListboxSelect>>', select)
会更好地解决这个问题。绑定到<<ListboxSelect>>
工作,因为此事件在选择更改后触发,而当您curselection()
使用此事件进行调用时,您将获得您正在寻找的正确输出。
Here is a bit of code that provides an example use of the <<ListboxSelect>>
event:
下面是一些提供<<ListboxSelect>>
事件使用示例的代码:
import tkinter as tk
class Application(tk.Frame):
def __init__(self, parent):
tk.Frame.__init__(self, parent)
self.parent = parent
self.lb = tk.Listbox(self.parent, height=4)
self.lb.pack()
self.lb.bind('<<ListboxSelect>>', self.print_weight)
for item in ["one: Index = 0", "two: Index = 1", "three: Index = 2", "four: Index = 3"]:
self.lb.insert("end", item)
def print_weight(self, event = None):
# [0] gets us the 1st indexed value of the tuple so weight == a number.
weight = self.lb.curselection()[0]
print(weight)
if __name__ == "__main__":
root = tk.Tk()
app = Application(root)
root.mainloop()
You will notice the print out in the console will be the current selected item on a single click. This will prevent the need for a double click.
您会注意到控制台中的打印输出将是单击时当前选定的项目。这将防止需要双击。