Welcome Guest ( Log In | Register )

Outline · [ Standard ] · Linear+

Python How many of you use TKinter ?, Python GUI

views
     
nzard
post Jun 27 2022, 07:10 PM

New Member
*
Newbie
17 posts

Joined: Feb 2017
From: A place where I don't belong.


save the trouble from using Tkinter and use PyQt5 instead. It has a GUI-based tool to facilitate your GUI design.


Used it for my Final Year Project and saved me a ton of work.
TSFlierMate1
post Jun 30 2022, 09:16 PM

Getting Started
**
Validating
139 posts

Joined: Jun 2022
I have question regarding Tkinter's Listbox selected value.

This line printed "3" if I selected row no. 4:

CODE
messagebox.showinfo('Test', listbox.curselection())


But ironically, this shows nothing no matter which row I select:

user posted image

(Even I treat ".curselection()" as string, also the same, empty output)


user posted image

Full source:
CODE
from tkinter import *
from tkinter import messagebox


user posted image

Does anyone know why? (Yeah, I think I will still stick to Tkinter after hearing your all opinions rolleyes.gif )

angch
post Jun 30 2022, 10:16 PM

On my way
****
Junior Member
636 posts

Joined: Jul 2006
QUOTE(FlierMate1 @ Jun 30 2022, 09:16 PM)
I have question regarding Tkinter's Listbox selected value.

This line printed "3" if I selected row no. 4:

CODE
messagebox.showinfo('Test', listbox.curselection())


But ironically, this shows nothing no matter which row I select:

user posted image

(Even I treat ".curselection()" as string, also the same, empty output)
 

user posted image

Full source:
CODE
from tkinter import *
from tkinter import messagebox


user posted image

Does anyone know why? (Yeah, I think I will still stick to Tkinter after hearing your all opinions  rolleyes.gif  )
*
Give lar copy/paste-able code, por favor!
angch
post Jun 30 2022, 10:37 PM

On my way
****
Junior Member
636 posts

Joined: Jul 2006
Meh. I Github Copiloted it.

CODE

from tkinter import *
from tkinter import messagebox

ws = Tk()
ws.title("Hello World")
ws.geometry("300x300")

listbox = Listbox(ws)

selection = ["hello", "world", "foo", "bar"]
for i, j in enumerate(selection):
   listbox.insert(i, j)

listbox.pack()

btn = Button(ws, text="Click Me", command=lambda: messagebox.showinfo('', listbox.get(listbox.curselection()))).pack()

ws.mainloop()


Don't lar use so much if/then/else, you're not a game programmer.

listbox.get() is used to shortcut unpack listbox.curselection() because it returns a list of selections (there can be more than one, ok, ok, it's technically a tuple, not a list), not a single selection.

The hint is in the source/documentation for curselection:
user posted image

It returns a tuple, or None.

e.g.
CODE
listbox = Listbox(ws, selectmode=MULTIPLE)


Full example:
CODE
from tkinter import *
from tkinter import messagebox

ws = Tk()
ws.title("Hello World")
ws.geometry("300x300")

listbox = Listbox(ws, selectmode=MULTIPLE)
for i, j in enumerate(["hello", "world", "foo", "bar"]):
   listbox.insert(i, j)
listbox.pack()

btn = Button(ws, text="Click Me", command=lambda: messagebox.showinfo("Hello"," ".join(map(lambda x: listbox.get(x), listbox.curselection())))).pack()

ws.mainloop()




This post has been edited by angch: Jun 30 2022, 11:10 PM
TSFlierMate1
post Jul 1 2022, 12:15 AM

Getting Started
**
Validating
139 posts

Joined: Jun 2022
QUOTE(angch @ Jun 30 2022, 10:16 PM)
Give lar copy/paste-able code, por favor!
*
Forum blocked me from posting code, and I have no idea which keyword triggered it. doh.gif

QUOTE(angch @ Jun 30 2022, 10:37 PM)
Don't lar use so much if/then/else, you're not a game programmer.

listbox.get() is used to shortcut unpack listbox.curselection() because it returns a list of selections (there can be more than one, ok, ok, it's technically a tuple, not a list), not a single selection.
......It returns a tuple, or None.
*
Yes, your example work, but I need to use if/then/else to evaluate the selected index (for single item listbox), so that I can assign a value based on the selected index.

This code doesn't work: icon_question.gif
CODE
   if listbox.get(listbox.curselection()) == 1:
       messagebox.showerror("Yeah, world")


...and also this not working (as pointed out in my previous post):
CODE
   if listbox.curselection() == 0:
       output = "Data Structure"


Any more idea?

This post has been edited by FlierMate1: Jul 1 2022, 12:20 AM
TSFlierMate1
post Jul 1 2022, 12:21 AM

Getting Started
**
Validating
139 posts

Joined: Jun 2022
I think I will end up with Radio button, much easier to get the selected value.
angch
post Jul 1 2022, 01:02 AM

On my way
****
Junior Member
636 posts

Joined: Jul 2006
I repeat.

listbox.curselection() returns a list of ints, not an int.

Of course if listbox.curselection() == 0 will always be false.

This post has been edited by angch: Jul 1 2022, 01:03 AM

 

Change to:
| Lo-Fi Version
0.0145sec    1.12    6 queries    GZIP Disabled
Time is now: 24th December 2025 - 11:24 PM