2012-12-06 107 views
0

所以我想建立一个GUI,我输入一些信息,清除输入字段,然后添加新的输入字段。但是,当我尝试通过grid_remove从根目录清除框架时,应用程序会冻结。相关代码如下。Tkinter冻结grid_remove

import tkinter 
from threading import Thread 

class PinGui(tkinter.Frame): 
    def __init__(self, client): 
     self.client = client 
     self.root = client.root 
     self.add_pin = client.add_pin 
     self.end = client.end 
     tkinter.Frame.__init__(self, self.root) 
     self.grid_widgets() 
     self.grid_buttons() 
     self.bind_keys() 
     self.grid(padx=32, pady=32) 

    def grid_buttons(self, b1='Add', b2='Reset', b3='Quit'): 
     self.addButton = tkinter.Button(self, text=b1, command=self.validate) 
     self.resetButton = tkinter.Button(self, text=b2, command=self.reset) 
     self.quitButton = tkinter.Button(self, text=b3, command=self.end) 
     self.buttons = [self.addButton, self.resetButton, self.quitButton] 
     for i in range(3): self.buttons[i].grid(row=i, column=11) 

    def grid_widgets(self): 
     widths = [3,3,4,4,6] 
     self.pin_vars = [] 
     self.pin_fields = [] 
     for i in range(5): 
      self.pin_vars.append(tkinter.StringVar()) 
      self.pin_fields.append(
       tkinter.Entry(self,width=widths[i], textvariable=self.pin_vars[i]) 
       ) 
      self.pin_fields[i].grid(row=0, column=2*i, padx=3) 
     self.pin_fields[0].focus_set() 

    def bind_keys(self): 
     self.root.bind_all("<Return>", self.validate) 
     self.root.bind_all("<Escape>", self.end) 

    def validate(self, args=None): 
     self.client.pin = [] 
     for field in self.pin_fields: 
      self.client.pin.append(field.get()) 
     Thread(target=self.add_pin).start() 

    def ungrid(self): 
     for field in self.pin_fields: field.grid_remove() 
     for button in self.buttons: button.grid_remove() 
     self.display.grid_remove() 

和:

class PinClient: 
    def __init__(self): 
     self.root = tkinter.Tk() 
     self.gui = PinGui(self) 
     self.pins = [] 

    def add_pin(self): 
     self.gui.reset() 
     if 'display' in self.__dict__: 
      self.pins.append(self.pin) 
      self.display.add_pin(self.pin) 
      self.ping("Enter PIN for Comp %s:" % len(self.display.col1)) 
      if len(self.display.col1) > 5: 
       self.end() # THIS IS WHERE IT FREEZES 
     else: 
      self.subject = self.pin 
      self.display = Display(self.root, self.pin) 
      self.display.grid(row=1, padx=32, pady=32) 
      self.ping("Enter PIN for Comp 1:") 

    def ping(self, msg): 
     self.gui.d_var.set(msg) 

    def end(self, args=None): 
     self.gui.ungrid() 

class Display(tkinter.Frame): 
    def __init__(self, master, pin): 
     tkinter.Frame.__init__(self, master) 
     self.pin = pin 
     self.col1 = [] 
     self.col2 = [] 
     self.col1.append(tkinter.Label(self, text="Subject:")) 
     self.col2.append(tkinter.Label(self, text=self.pin)) 
     self.grid_widgets() 

    def grid_widgets(self): 
     self.ungrid() 
     for i in range(len(self.col1)): 
      self.col1[i].grid(row=i, column=0) 
      self.col2[i].grid(row=i, column=1) 

    def ungrid(self): 
     for i in range(len(self.col1)): 
      self.col1[i].grid_remove() 
      self.col2[i].grid_remove() 

    def add_pin(self, pin): 
     self.col1.append(tkinter.Label(self, text="Comp %s:" % len(self.col1))) 
     self.col2.append(tkinter.Label(self, text=pin)) 
     i = len(self.col1) 
     self.col1[i-1].grid(row=i, column=0) 
     self.col2[i-1].grid(row=i, column=1) 

这似乎是主题相关的线程,但我一直没能找到任何原因,这应该冻结。任何帮助是极大的赞赏!

回答

0

Tkinter不是线程安全的。如果你在除了接触GUI对象的主线程之外的线程中做任何事情,那么你将得到不可预知的结果。几乎可以肯定的是,由于您试图从工作者线程访问小部件,这是导致您的问题的线程。

+0

其实这个问题出现之前,我尝试线程功能。真正的问题是它不在* correct *线程(即主循环)中。我通过发送ungrid命令到GUI中的队列解决了这个问题,GUI然后从主循环内发出命令。 –