Hello,

I'm trying to understand how to use a tkinter listbox. It should show a list of strings; the selected string should be transferred into an entry field where it can be edited and written back into the listbox.

At the moment I'm just trying to read and change listbox entries programmatically. Here is the first attempt:

# listbox_mini.py

import tkinter as tk
from tkinter import ttk

# *** see (a)
cFlowers = ("Iberis sempervirens", "Geranium macrorrhizu", "Nemesia Sunsatia",
            "Iris Barbata Nana")

class MainWindow(ttk.Frame):

     def __init__(self, master=None):
         super().__init__(master)
         top = self.winfo_toplevel()
         top.rowconfigure(0, weight=1)
         top.columnconfigure(0, weight=1)
         self.rowconfigure(0, weight=1)
         self.columnconfigure(0, weight=1)
         self.grid(sticky="nsew")
         self.createListboxFrame(cFlowers)

     def createListboxFrame(self, vals):
         lboxfr = ttk.Frame(self)
         lboxfr.rowconfigure(0, weight=1)
         lboxfr.columnconfigure(1, weight=1)
         lboxfr.grid(sticky="nsew")
         lb = ttk.Label(lboxfr, text='Flowers')
         lb.grid(padx=5, pady=5)
         # *** next two lines: see (b)
         # self.flowers = tk.Variable(value=vals)
         self.flowers = tk.StringVar(value=vals)
         self.flowerbox = tk.Listbox(lboxfr, listvariable=self.flowers, height=5)
         self.flowerbox.grid(row=0, column=1, padx=5, pady=5, sticky="nsew")
         self.flowerbox.bind("<<ListboxSelect>>", self.doEdit)

    def doEdit(self, evt):
         print(self.flowerbox.curselection())
         idx = self.flowerbox.curselection()[0]
         # *** next two lines: see (b)
         # xx = self.flowers.get()[int(idx)]
         xx = self.flowerbox.get(idx)
         print(repr(xx))
         # self.flowers[int(idx)].set(xx + '**')
         # *** TypeError: "StringVar" object (or "Variable object")
         # *** does not support indexing
         # self.flowerbox.set(idx, xx + '**')
         # *** AttributeError: "Listbox" object has no attribute "set"
         self.flowerbox.delete(idx)
         self.flowerbox.insert(idx, xx + '**')
         print(repr(self.flowers.get()))

def main():

    mw =
MainWindow()
    mw.master.title("Listbox and listvariable")
    mw.mainloop()

if __name__ == '__main__':
    main()


Questions about this:


(a) If and only if cFlowers is a tuple the elements of this tuple are shown as lines in the listbox. If it's a list the lines in the listbox are the elements of the list returned by repr(cFlowers).split(). Why?

(b) If self.flowers is a tk.StringVar() then self.flowers.get()[int(idx)] doesn't return the listbox entry at index idx, but the character at index idx in the string repr(cFlowers). If it's a tk.Variable(), then this construction returns the listbox entry. But it is quite ugly, anyway. The other line, "xx = self.flowerbox.get(idx)" always returns the listbox entry.

(c) Replacing a listbox entry only seems to be possible by deleting the old and inserting the new one. Why? And only with listbox methods, not by working with the listvariable.

I suppose it would be possible to do any changes to the list using cFlowers directly (as a list, not as a tuple, of course) and replacing the entire self.flowers by the changed version, converted to a tuple, as often as necessary.

But all of this leads to the question: why use the listvariable at all? Or what other tkinter control would be better for my purpose?

Googling only showed some older quite similar questions without answers.

Using Python 3.1.2 with Tcl/tk 8.5, tkinter revision 73084.

Thank you for explanations,
Sibylle

  

GRATIS für alle WEB.DE Nutzer: Die maxdome Movie-FLAT!   
Jetzt freischalten unter http://movieflat.web.de
_______________________________________________
Tkinter-discuss mailing list
Tkinter-discuss@python.org
http://mail.python.org/mailman/listinfo/tkinter-discuss

Reply via email to