Posts: 22
Threads: 7
Joined: Feb 2020
Feb-15-2020, 10:35 PM
(This post was last modified: Feb-15-2020, 11:22 PM by Larz60+.)
Hi,
how can I convert the selected item from Listbox and convert into integer for using in my code?
Here is the code:
I already converted the item into an integer and I can return it with a new value, but how can I use this value in my code? I've tried anything I can imagine, for example I used the variable b, but I always get an error:
Posts: 22
Threads: 7
Joined: Feb 2020
Feb-15-2020, 10:51 PM
(This post was last modified: Feb-15-2020, 11:32 PM by Larz60+.)
(Sorry, I've missed the code, here is the proper post:)
Hi,
how can I convert the selected item from Listbox and convert into integer for using in my code?
Here is the code:---------------------------------------------------------------------------------------------
from tkinter import*
root=Tk()
sizex = 600
sizey = 400
posx = 40
posy = 20
root.wm_geometry("%dx%d+%d+%d" % (sizex, sizey, posx, posy))
def Select(a):
a = int(mylistbox.get(ANCHOR))
if a == 1:
a = 2
return(a)
elif a == 2:
a = 3
return(a)
mylistbox=Listbox(root,height=2, width=25, font=('times',13))
mylistbox.bind('<<ListboxSelect>>',Select)
#mylistbox.insert(1,2)
itemsforlistbox=['1','2']
mylistbox.place(x=32,y=90)
scroll = Scrollbar(root, orient=VERTICAL) # the allignment of the scrollbar
mylistbox["yscrollcommand"]=scroll.set # link the list with the scroll
scroll["command"]=mylistbox.yview # link the scroll with the scroll
mylistbox.grid(row=5, column=5)
scroll.grid(row=5, column=5, sticky=N+S+E)
for items in itemsforlistbox:
mylistbox.insert(END,items)
root.mainloop() -----------------------------------------------------------------------------------------------------------------------------------
I already converted the item into an integer and I can return it with a new value, but how can I use this value in my code? I've tried anything I can imagine, for example I used the variable b, but I always get an error:
---------------------------------------------------------------------------------------------------------------------------------------
from tkinter import*
root=Tk()
sizex = 600
sizey = 400
posx = 40
posy = 20
root.wm_geometry("%dx%d+%d+%d" % (sizex, sizey, posx, posy))
def Select(a):
a = int(mylistbox.get(ANCHOR))
if a == 1:
a = 2
return(a)
elif a == 2:
a = 3
return(a)
b = Select(a) + 2
mylistbox=Listbox(root,height=2, width=25, font=('times',13))
mylistbox.bind('<<ListboxSelect>>',Select)
#mylistbox.insert(1,2)
itemsforlistbox=['1','2']
mylistbox.place(x=32,y=90)
scroll = Scrollbar(root, orient=VERTICAL) # the allignment of the scrollbar
mylistbox["yscrollcommand"]=scroll.set # link the list with the scroll
scroll["command"]=mylistbox.yview # link the scroll with the scroll
mylistbox.grid(row=5, column=5)
scroll.grid(row=5, column=5, sticky=N+S+E)
for items in itemsforlistbox:
mylistbox.insert(END,items)
root.mainloop()
Posts: 22
Threads: 7
Joined: Feb 2020
Feb-15-2020, 10:59 PM
(This post was last modified: Feb-15-2020, 11:52 PM by Jionni.)
Hi,
how can I convert the selected item from Listbox and convert into integer for using in my code?
Here is the code:
from tkinter import*
root=Tk()
sizex = 600
sizey = 400
posx = 40
posy = 20
root.wm_geometry("%dx%d+%d+%d" % (sizex, sizey, posx, posy))
def Select(a):
a = int(mylistbox.get(ANCHOR))
if a == 1:
a = 2
return(a)
elif a == 2:
a = 3
return(a)
mylistbox=Listbox(root,height=2, width=25, font=('times',13))
mylistbox.bind('<<ListboxSelect>>',Select)
#mylistbox.insert(1,2)
itemsforlistbox=['1','2']
mylistbox.place(x=32,y=90)
scroll = Scrollbar(root, orient=VERTICAL) # the allignment of the scrollbar
mylistbox["yscrollcommand"]=scroll.set # link the list with the scroll
scroll["command"]=mylistbox.yview # link the scroll with the scroll
mylistbox.grid(row=5, column=5)
scroll.grid(row=5, column=5, sticky=N+S+E)
for items in itemsforlistbox:
mylistbox.insert(END,items)
root.mainloop() I already converted the item into an integer and I can return it with a new value, but how can I use this value in my code? I've tried anything I can imagine, for example I used the variable b, but I always get an error:
from tkinter import*
root=Tk()
sizex = 600
sizey = 400
posx = 40
posy = 20
root.wm_geometry("%dx%d+%d+%d" % (sizex, sizey, posx, posy))
def Select(a):
a = int(mylistbox.get(ANCHOR))
if a == 1:
a = 2
return(a)
elif a == 2:
a = 3
return(a)
b = Select(a) + 2
mylistbox=Listbox(root,height=2, width=25, font=('times',13))
mylistbox.bind('<<ListboxSelect>>',Select)
#mylistbox.insert(1,2)
itemsforlistbox=['1','2']
mylistbox.place(x=32,y=90)
scroll = Scrollbar(root, orient=VERTICAL) # the allignment of the scrollbar
mylistbox["yscrollcommand"]=scroll.set # link the list with the scroll
scroll["command"]=mylistbox.yview # link the scroll with the scroll
mylistbox.grid(row=5, column=5)
scroll.grid(row=5, column=5, sticky=N+S+E)
for items in itemsforlistbox:
mylistbox.insert(END,items)
root.mainloop()
Does anybody have a clue, how I can solve this??
Posts: 74
Threads: 0
Joined: Mar 2017
Hi Jionni
Please try out the following snippet:
from tkinter import*
root=Tk()
sizex = 600
sizey = 400
posx = 40
posy = 20
root.wm_geometry("%dx%d+%d+%d" % (sizex, sizey, posx, posy))
def Show(event):
global b
print(b)
def Select(event):
global b
a = int(mylistbox.get(ANCHOR))
if a == 1:
a = 2
elif a == 2:
a = 3
b = a + 2
result = 0
itemsforlistbox=['1','2']
mylistbox = Listbox(root, height=2, width=25, font=('times',13))
mylistbox.bind('<<ListboxSelect>>', Select)
mylistbox.insert(END, *itemsforlistbox)
mylistbox.place(x=32,y=90)
scroll = Scrollbar(root, orient=VERTICAL) # the allignment of the scrollbar
mylistbox["yscrollcommand"]=scroll.set # link the list with the scroll
scroll["command"]=mylistbox.yview # link the scroll with the scroll
mylistbox.grid(row=5, column=5)
scroll.grid(row=5, column=5, sticky=N+S+E)
root.bind('<Return>', Show)
root.mainloop() wuf :-)
Posts: 22
Threads: 7
Joined: Feb 2020
Hi wuf!!
Ok, first of all, thanks for answering! Now, using your code, the global variable b doesn´t show up in my console. Only if I say: print(b) und put it below: b = a + 2, then b displays.
But actually, I want to return the value of b and use it, for example: c = b + 2. But it doesn't work. It is always the same error: NameError: name 'b' is not defined.
from tkinter import*
root=Tk()
sizex = 600
sizey = 400
posx = 40
posy = 20
root.wm_geometry("%dx%d+%d+%d" % (sizex, sizey, posx, posy))
def Show(event):
global b
print(b)
def Select(event):
global b
a = int(mylistbox.get(ANCHOR))
if a == 1:
a = 2
elif a == 2:
a = 3
b = a + 2
print(b)
c = b + 2 # doesn't work
result = 0
itemsforlistbox=['1','2']
mylistbox = Listbox(root, height=2, width=25, font=('times',13))
mylistbox.bind('<<ListboxSelect>>', Select)
mylistbox.insert(END, *itemsforlistbox)
mylistbox.place(x=32,y=90)
scroll = Scrollbar(root, orient=VERTICAL) # the allignment of the scrollbar
mylistbox["yscrollcommand"]=scroll.set # link the list with the scroll
scroll["command"]=mylistbox.yview # link the scroll with the scroll
mylistbox.grid(row=5, column=5)
scroll.grid(row=5, column=5, sticky=N+S+E)
root.bind('<Return>', Show)
root.mainloop()
Posts: 74
Threads: 0
Joined: Mar 2017
OK Jionni
May be this script does show more about the situation:
from tkinter import*
root=Tk()
sizex = 600
sizey = 400
posx = 100
posy = 100
root.wm_geometry("%dx%d+%d+%d" % (sizex, sizey, posx, posy))
b = 0
# This function 'select' is reserved for the listbox select event only!
# That means this function is called as soon you select an item in the listbox
# The parameter 'a' is wrong. It is called 'event'
#def Select(a):
def select(event):
global b
a = int(mylistbox.get(ANCHOR))
b = calculation(a) + 2
# Now the global variable 'b' is set to the result of your calculation
def calculation(a):
if a == 1:
a = 2
return(a)
elif a == 2:
a = 3
return(a)
def show_var_b(event):
global b
print("The value of variable 'b' is {}".format(b))
# As above explained 'select' is reserved for the listbox select event an not
# for a call from the global environment
#b = Select(a) + 2
mylistbox=Listbox(root,height=2, width=25, font=('times',13))
mylistbox.bind('<<ListboxSelect>>', select)
#mylistbox.insert(1,2)
itemsforlistbox=['1','2']
mylistbox.place(x=32,y=90)
scroll = Scrollbar(root, orient=VERTICAL) # the allignment of the scrollbar
mylistbox["yscrollcommand"]=scroll.set # link the list with the scroll
scroll["command"]=mylistbox.yview # link the scroll with the scroll
mylistbox.grid(row=5, column=5)
scroll.grid(row=5, column=5, sticky=N+S+E)
for items in itemsforlistbox:
mylistbox.insert(END,items)
# After you have selected an item in the listbox the global variable 'b' is
# available for further usage. By pressing 'Return' (Enter) on your keyboard
# the result of the global variable 'b' is printed out.
root.bind('<Return>', show_var_b)
root.mainloop() wuf :-)
Posts: 22
Threads: 7
Joined: Feb 2020
Hey wuf,
this help me a lot, you are amazing... But there is something I don't get yet.
How can I use the variable b for further coding? Ok, I can print out the result by pressig the ENTER-button, thats really nice and useful. But how can I assign the variable b, for example, to variable z, or taking the value of b and use it for another function.
You know what I mean? I want to use the value of b in another part of the code...
Posts: 74
Threads: 0
Joined: Mar 2017
Ok Jionni
Lets make a simple application. By activating the button the app will multiply your listbox selection witch is stored in the variable 'b' with the constant PI:
import tkinter as tk
# Constants for the geometry of the main window
APP_XPOS = 100
APP_YPOS = 100
APP_WIDTH = 300
APP_HEIGHT = 300
APP_TITLE = "Simple Applikation"
PI = 3.1415
# Variables
b = 0
items_for_listbox = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
# Functions
def select(event):
global b
b = int(mylistbox.get(tk.ANCHOR))
selected_var.set(b)
def calculate():
global b
result = b * PI
result_var.set(result)
# GUI
# App window
app_win = tk.Tk()
app_win.title(APP_TITLE)
app_win.wm_geometry("%dx%d+%d+%d" % (APP_WIDTH, APP_HEIGHT, APP_XPOS, APP_YPOS))
# Main container embeded in the app window
# Container for all of the app widgets
main_frame = tk.Frame(app_win)
main_frame.pack(expand=tk.YES, fill=tk.BOTH)
# Label to show your listbox selection
selected_var = tk.StringVar()
tk.Label(main_frame, textvariable=selected_var, font=('times', 30, 'bold')
).pack(expand=tk.YES)
selected_var.set("No selection")
# Label to show your calculated result
result_var = tk.StringVar()
tk.Label(main_frame, textvariable=result_var, font=('times', 30, 'bold')
).pack(expand=tk.YES)
result_var.set("No result")
# Container for the listbox & scrollbar (embeded in the main frame)
listbox_frame = tk.Frame(main_frame)
listbox_frame.pack(expand=tk.YES)
mylistbox = tk.Listbox(listbox_frame, height=5, width=25, font=('times',13))
mylistbox.bind('<<ListboxSelect>>', select)
mylistbox.grid(row=0, column=0)
mylistbox.insert(tk.END, *items_for_listbox)
scroll = tk.Scrollbar(listbox_frame, orient=tk.VERTICAL) # the allignment of the scrollbar
mylistbox["yscrollcommand"] = scroll.set # link the list with the scroll
scroll["command"] = mylistbox.yview # link the scroll with the scroll
scroll.grid(row=0, column=1, sticky=tk.N+tk.S) #sticky=N+S+E)
# Button to launch a calculation
tk.Button(main_frame, text="Calculate (Listbox selection * PI)",
command=calculate).pack(expand=tk.YES)
app_win.mainloop() wuf :-)
Posts: 22
Threads: 7
Joined: Feb 2020
Wow... now I got it, I think!!hahaha
The thing is I've written a code for calculating the reverberation time in a room. So, the code was not the problem. I've never thought, that the gui takes so many time...
In the next weeks, I will ask further questions about the gui and I hope you will help me again. :)
Thanks!!
|