Python Forum
tkinter| listbox.insert problem
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
tkinter| listbox.insert problem
#1
Hi! I'm beginner with tkinter, I have one question.
When I select item from the list, I want to copy it and added again to the same list. However, the list is updating with numbers only. Line 4:

def add_me():
    selected_items = listbox.curselection()
    for item in selected_items:
        listbox.insert(END, item)
        print(item, 'is added')


listbox = Listbox(root, width = 40, height = 15, selectmode = MULTIPLE)
listbox.insert(0, 'Python')
listbox.insert(1, 'C++')
listbox.insert(2, 'C#')
listbox.insert(3, 'PHP')
listbox.pack(pady = 25)

btn = Button(root, text = 'Print', command = print_me).place(x = 200, y = 300)
btn2 = Button(root, text = 'Delete', command = delete_me).place(x = 300, y = 300)
btn3 = Button(root, text = 'Add', command = add_me).place(x = 400, y = 300)
Output:
2 is added
2 is added
3 is added


I will appreciate any help, Thank you!
Reply
#2
please show enough code so that we can run it.
Reply
#3
(Sep-29-2020, 03:24 PM)Larz60+ Wrote: please show enough code so that we can run it.

from tkinter import *
from tkinter import ttk
root = Tk()



def print_me():
    selected_items = listbox.curselection()
    for item in selected_items:
        print(listbox.get(item))


def delete_me():
    selected_items = listbox.curselection()
    for item in selected_items:
        listbox.delete(item)


def add_me():
    selected_items = listbox.curselection()
    for item in selected_items:
        listbox.insert(END, item)
        print(item, 'is added')


listbox = Listbox(root, width = 40, height = 15, selectmode = MULTIPLE)
listbox.insert(0, 'Python')
listbox.insert(1, 'C++')
listbox.insert(2, 'C#')
listbox.insert(3, 'PHP')
listbox.pack(pady = 25)

btn = Button(root, text = 'Print', command = print_me).place(x = 200, y = 300)
btn2 = Button(root, text = 'Delete', command = delete_me).place(x = 300, y = 300)
btn3 = Button(root, text = 'Add', command = add_me).place(x = 400, y = 300)


root.geometry('650x450+650+350')
root.mainloop()
Reply
#4
Just like in the print_me function where you get the value corresponding to the given index, you need to do that in the function add_me
Change
def add_me():
    selected_items = listbox.curselection()
    for item in selected_items:
        listbox.insert(END, item)
        print(item, 'is added')

to
def add_me():
    selected_items = listbox.curselection()
    for item in selected_items:
        item_value = listbox.get(item)
        listbox.insert(END, item_value)
        print(item_value, 'is added')
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] How to insert data json to treeview tkinter? Shakanrose 8 4,213 Jan-19-2023, 03:58 PM
Last Post: Shakanrose
  Python3 tkinter radiobutton problem Nick_tkinter 14 5,833 Feb-15-2021, 11:01 PM
Last Post: Nick_tkinter
  tkinter python button position problem Nick_tkinter 3 3,486 Jan-31-2021, 05:15 AM
Last Post: deanhystad
  [Tkinter] ClockIn/Out tkinter problem Maryan 2 2,165 Oct-12-2020, 03:42 AM
Last Post: joe_momma
  What units does the width parameter of a tkinter Listbox use? Pedroski55 2 5,450 Jul-04-2020, 08:17 AM
Last Post: deanhystad
  Tkinter problem DPaul 6 4,048 May-28-2020, 03:40 PM
Last Post: DPaul
  [Tkinter] Tkinter - I have problem after import varaible or function from aGUI to script johnjh 2 2,523 Apr-17-2020, 08:12 PM
Last Post: johnjh
  [Tkinter] Problem with tkinter when creating .exe file Jan_97 2 4,540 Feb-27-2020, 05:17 PM
Last Post: Jan_97
  [Tkinter] Tkinter problem catlessness 1 2,013 Jan-15-2020, 05:17 AM
Last Post: Larz60+
  Problem with Submit button Tkinter Reldaing 2 3,610 Jan-05-2020, 01:58 AM
Last Post: balenaucigasa

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020