Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
244 views
in Technique[技术] by (71.8m points)

python - Saving selected items from a listbox for use in another class

I'm writing a portion of code which is supposed to behave the following way:

  1. Display a listbox of items.
  2. The user selects one or more items from the listbox.
  3. The user clicks a seperate button widget to confirm the selection.
  4. The toplevel window closes once the user clicked the button.
  5. The selection is saved so that other portions of the program can access it.

I have the following code (code not relevant to the question is omitted):

class MainWin(tk.Tk):
   # ... omitted code

    def dia(self):
        '''
        This creates a toplevel window that displays the listbox.
        '''
        TL = dialogWin(self)
        MainWin.wait_window(TL)

class dialogWin(tk.Toplevel):
    '''
    This window creates and displays the listbox.
    '''
    def __init__(self, MainWin):
       # ... omitted code
       # ...
        B = tk.Button(self, text = 'Confirm', command = self.getChoices).grid(row = 3) #This is the button that the user clicks to confirm the selection
        
        #create listbox
        self.LB = tk.Listbox(self, height = 10, width = 45, selectmode = 'multiple')
      #  ... code to configure the listbox appearence
    
    def getChoices(self):
        '''
        This is the callback function the listbox uses.
        '''
        choice = self.LB.curselection()
        self.destroy()
        return choice                  

That's the code that I have but I can't seem to find a way to actually access the information stored in choice. For instance if I edit dia(self) to confirm the return value, like so:

def dia(self):
        TL = dialogWin(self)
        MainWin.wait_window(TL)
        print(TL.getChoices())

I get the error _tkinter.TclError: invalid command name ".!dialogwin.!listbox". TL.getChoices().get() gives me the same error. What I want is the selected information to be saved as something like (1, 2, 3) where I can then access the tuple from the main window. Is there something I've done wrong here?

question from:https://stackoverflow.com/questions/65946783/saving-selected-items-from-a-listbox-for-use-in-another-class

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You are very close. You can no longer use tkinter widget methods after a window is destroyed, but you can use your own methods and attributes:

import tkinter as tk

class MainWin(tk.Tk):
    def __init__(self):
        super().__init__()
        tk.Button(self, text='click me', command=self.dia).pack()
        self.geometry('200x200')

    def dia(self):
        TL = dialogWin(self)
        MainWin.wait_window(TL)
        print(TL.choice)

class dialogWin(tk.Toplevel):
    def __init__(self, MainWin):
        super().__init__(MainWin)

        B = tk.Button(self, text = 'Confirm', command = self.getChoices).grid(row = 3) #This is the button that the user clicks to confirm the selection

        #create listbox
        self.LB = tk.Listbox(self, height = 10, width = 45, selectmode = 'multiple')
        self.LB.grid()
        for x in range(10):
            self.LB.insert(tk.END, f"this is item {x}")

    def getChoices(self):
        self.choice = self.LB.curselection() # <== save the choice as an attribute
        self.destroy()

MainWin().mainloop()

You could also save it as an attribute of the main window if you wanted to:

    def getChoices(self):
        self.master.choice = self.LB.curselection()
        self.destroy()

Or any other place really.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...