返回tkinter listbox结果到主脚本



我是tkinter/python的新手,我担心我的问题是如此简单,没有人被愚弄了。我创建了以下脚本:

import tkinter as tk
from tkinter import *
class example(tk.Frame):
    #Initialize class
    def __init__(self,parent):
        tk.Frame.__init__(self, parent)
        self.listbox = tk.Listbox(self,selectmode=SINGLE)
        self.listbox.pack(expand=True)
        self.button = tk.Button(self,text="Confirm Selection",command=self.selection)
        self.button.pack(side="bottom",fill="both",expand=True)
        self.string = tk.StringVar()
        #Add variables
        for i in ['A','B','C']:
            self.listbox.insert(END,i)
    #Selection method
    def selection(self):
        index = self.listbox.curselection()[0]
        name = ['A','B','C'][index]
        self.string = name
        print(self.string)
#Main script
root = tk.Tk()
example(root).pack(expand=True)
root.mainloop()

我能够在

中打印列表选择的结果
def selection(self)

但是,随着脚本继续运行,我不知道该如何撤出并进一步使用。例如,如果要继续我的#main脚本,我想做类似的事情:

letter = '''results of listbox selection'''

作为奖励,我很难销毁列表框,只有列表和我尝试过的方法的按钮被删除。

谢谢大家

我已经解决了我的问题。我不知道如何在返回值后正确使用wait_window函数恢复脚本。

我已经进行了大量修改,以下是我的目的:

from tkinter import *
class ListBoxChoice(object):
    def __init__(self, master=None, title=None, message=None, list=[]):
        self.master = master
        self.value = None
        self.list = list[:]
        
        self.modalPane = Toplevel(self.master)
        self.modalPane.transient(self.master)
        self.modalPane.grab_set()
        self.modalPane.bind("<Return>", self._choose)
        self.modalPane.bind("<Escape>", self._cancel)
        if title:
            self.modalPane.title(title)
        if message:
            Label(self.modalPane, text=message).pack(padx=1, pady=1)
        listFrame = Frame(self.modalPane)
        listFrame.pack(side=TOP, padx=5, pady=5)
        
        scrollBar = Scrollbar(listFrame)
        scrollBar.pack(side=RIGHT, fill=Y)
        self.listBox = Listbox(listFrame, selectmode=SINGLE)
        self.listBox.pack(side=LEFT, fill=Y)
        scrollBar.config(command=self.listBox.yview)
        self.listBox.config(yscrollcommand=scrollBar.set)
        self.list.sort()
        for item in self.list:
            self.listBox.insert(END, item)
        buttonFrame = Frame(self.modalPane)
        buttonFrame.pack(side=BOTTOM)
        chooseButton = Button(buttonFrame, text="Choose", command=self._choose)
        chooseButton.pack()
        cancelButton = Button(buttonFrame, text="Cancel", command=self._cancel)
        cancelButton.pack(side=RIGHT)
    def _choose(self, event=None):
        try:
            firstIndex = self.listBox.curselection()[0]
            self.value = self.list[int(firstIndex)]
        except IndexError:
            self.value = None
        self.modalPane.destroy()
    def _cancel(self, event=None):
        self.modalPane.destroy()
        
    def returnValue(self):
        self.master.wait_window(self.modalPane)
        return self.value
        self.modalPane.destroy()
        
if __name__ == '__main__':
    root = Tk()
    list = ['A','B','C']
    returnValue = ListBoxChoice(root, "Pick Letter", "Pick one of these crazy random numbers", list).returnValue()
    print(returnValue)'

我现在唯一的问题是我在运行脚本时打开2个对话框。我敢肯定,这与我如何约束所有内容有关,但仍在整理。

最新更新