2016-05-13 43 views
1

使用wait_window()我想我的Tkinter的應用程序從一個叫窗口中返回一個列表框的選擇項目時(類MyDialog)返回一個列表框的選擇的項目到調用窗口(類示例)。如果我不使用wait_window(),則返回一個空列表。使用wait_window()會導致錯誤消息。在我看來,wait_window()阻止了curselection()方法。需要改變什麼以獲得適當的回報?如何Tkinter的

以下示例是this answer的修改版本。

import tkinter as tk 

class MyDialog(object): 
    def __init__(self, parent): 
     self.toplevel = tk.Toplevel(parent) 

     choices = ("one", "two","three") 
     names = tk.StringVar(value=choices) 

     label = tk.Label(self.toplevel, text="Pick something:") 
     self.listbox = tk.Listbox(self.toplevel, listvariable=names, height=3, 
      selectmode="single", exportselection=0) 
     button = tk.Button(self.toplevel, text="OK", command=self.toplevel.destroy) 

     label.pack(side="top", fill="x") 
     self.listbox.pack(side="top", fill="x") 
     button.pack() 

    def show(self): 
     self.toplevel.wait_window() 

     value = self.listbox.curselection() 
     return value 

class Example(tk.Frame): 
    def __init__(self, parent): 
     tk.Frame.__init__(self, parent) 

     self.button = tk.Button(self, text="Click me!", command=self.on_click) 
     self.label = tk.Label(self, width=80) 
     self.label.pack(side="top", fill="x") 
     self.button.pack(pady=20) 

    def on_click(self): 
     result = MyDialog(self).show() 
     self.label.configure(text="your result: %s" % result) 

if __name__ == "__main__": 
    root = tk.Tk() 
    Example(root).pack(fill="both", expand=True) 
    root.mainloop() 

回答

1

wait_window等待,直到寡婦是破壞。而且由於它被銷燬,所以你不能從小部件中獲取任何信息。

您必須設置綁定,以便在數據更改時將數據保存到變量中,以便在小部件被銷燬後可以獲取它。

+0

我明白了,我會盡力的。但是爲什麼返回工作與原始示例中的選項菜單一樣? – AnBe

+0

@AnBe:因爲選項菜單將該值存儲在一個變量中,該變量在該構件被銷燬時被_not_銷燬。 –

0

布萊恩奧克利的回答讓我找到了下面的解決方案。非常感謝你!

import tkinter as tk 

class MyDialog(object): 
    def __init__(self, parent): 
     self.toplevel = tk.Toplevel(parent) 

     choices = ("one", "two","three") 
     names = tk.StringVar(value=choices) 

     label = tk.Label(self.toplevel, text="Pick something:") 
     self.listbox = tk.Listbox(self.toplevel, listvariable=names, height=3, 
      selectmode="single", exportselection=0) 
     button = tk.Button(self.toplevel, text="OK", command=self.toplevel.destroy) 

     label.pack(side="top", fill="x") 
     self.listbox.pack(side="top", fill="x") 
     button.pack() 

     # add binding 
     self.listbox.bind('<<ListboxSelect>>', self.getSelection) 

    # function associated with binding 
    def getSelection(self, event): 
     widget = event.widget 
     selection=widget.curselection() 
     self.value = widget.get(selection[0]) 

    # separate function for wait_window and the return of the selection 
    def returnValue(self): 
     self.toplevel.wait_window() 
     return self.value 

class Example(tk.Frame): 
    def __init__(self, parent): 
     tk.Frame.__init__(self, parent) 

     self.button = tk.Button(self, text="Click me!", command=self.on_click) 
     self.label = tk.Label(self, width=80) 
     self.label.pack(side="top", fill="x") 
     self.button.pack(pady=20) 

    def on_click(self): 
     result = MyDialog(self).returnValue() 
     self.label.configure(text="your result: %s" % result) 

if __name__ == "__main__": 
    root = tk.Tk() 
    Example(root).pack(fill="both", expand=True) 
    root.mainloop()