將tkinter listbox結果返回給主腳本

[英]Return tkinter listbox result to main script


I am new to tkinter/python and I fear my question is so simple, no one was dumbed it down to this point. I have created the following script:

我是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()

I am able to print the result of my list selection within

我能夠打印出列表選擇的結果

def selection(self)

However, I do not know how to pull out and use further as the script continues to run. For example, if my #Main script were to be continued, I would like to do something like:

但是,我不知道如何在腳本繼續運行時拉出並進一步使用。例如,如果要繼續我的#Main腳本,我想做的事情如下:

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

As a bonus, I am having difficulty destroying the listbox, with only the list and the button being removed for the methods I have tried.

作為獎勵,我很難銷毀列表框,只有列表和按鈕被刪除我嘗試過的方法。

Thanks everyone

1 个解决方案

#1


0  

I have resolved my issue. I was unaware of how to properly use the wait_window function to resume the script once a value is returned.

我已經解決了我的問題。一旦返回值,我不知道如何正確使用wait_window函數來恢復腳本。

I have modified substantially and the below fits my purposes:

我已經進行了大幅修改,以下內容符合我的目的:

`
from tkinter import *

`來自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)'

The only issue I have now is am I getting 2 dialog boxes open when I run the script. I'm sure it has something to do with how I am binding everything, but still sorting.

我現在唯一的問題是,當我運行腳本時,我打開了2個對話框。我確定它與我如何綁定所有內容有關,但仍在排序。

Cheers


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2018/03/27/720b8ac6491cd3e1a1f67537d4aeb308.html



 
粤ICP备14056181号  © 2014-2020 ITdaan.com