Convert tuple to int in Python

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

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.

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'

2

2 Answers

what you're looking for is

weight = int(weight[0])

int is a function that returns an integer, so you have to assign that return to a variable.

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.

If the item is already an integer then the int call might be redundant, you might be able to get it with just

weight = weight[0]
1

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.

Here is a bit of code that provides an example use of the <<ListboxSelect>> event:

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.

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like