首页 文章

暂停python tkinter中的事件

提问于
浏览
0

我在覆盆子pi上使用python 2.7 tkinter gui来自动化一些材料测试 . 对于测试,必须测试多个样品,并且交换样品需要时间 . 我想提示文字说“请插入样品一,然后按键盘上的输入”,然后让功能暂停,直到输入被按下 . 而不是按Enter键我也可以使用tkinter按钮 . 没有使用外部库的任何想法?我尝试了一个while循环,我按下按钮后尝试退出循环,但由于循环正在运行,按钮不会注册 .

示例代码(删除了大量代码并留下相关内容):

class App:
def __init__(self,master):

    #self.WILTRON = Wiltron_54128A_GPIB()

    self.var = tk.StringVar()
    self.var.trace("w",self.getTest)

    self.okbutton = tk.IntVar()
    self.okbutton.trace("w",self.OKbutton)

    frame = Frame(master)
    frame.pack()

    #Initial GUI values
    self.var.set('Choose Test')
    testChoices = ['TEST']
    App.testOption = tk.OptionMenu(frame, self.var, *testChoices)
    App.testOption.grid(row=0, column=0)
    okButton = tk.Button(frame, text="     OK    ", command=self.OKbutton).grid(row=2, column=1)

#Test routine functions
def getTest(self, *args):
    test = self.var.get()
    sf = "IC Network Analyzer"
    root.title(sf)

    #####
    if test == "TEST":
        sample1 = self.WILTRON.Sample_Data()
        print 'Change out sample then press OK'
        #This is where I need to pause until the next sample has been inserted
        sample2 = self.WILTRON.Sample_Data()
        #ect.
    #####

def OKbutton(self):
    #Whatever I need to do to make the button exit the pause

2 回答

  • 2

    使用tkMessageBox

    import Tkinter
    import tkMessageBox
    
    print "Sample #1"
    tkMessageBox.showinfo("Message", "Insert sample and press OK")
    print "Sample #2"
    
  • 1

    这是一个使用回调来启动测试的工作示例,以及一个用于推进每个样本的回调 .

    import Tkinter as tk
    
    class App:
        def __init__(self, master):
            self.master = root
            self.frame = tk.Frame(self.master)
    
            self.okLabel = tk.Label(self.frame, text="Change out sample then press OK")
            self.okButton = tk.Button(self.frame, text="     OK    ", command=self.nextSample)
    
            self.var = tk.StringVar()
            self.var.set('Choose Test')
            self.var.trace("w",self.beginTest)
            testChoices = ['TEST']
            self.testOption = tk.OptionMenu(self.frame, self.var, *testChoices)
    
            self.sampleNumber = 1
            self.maxSamples = 5
            self.testing = False
            self.samples = []
    
            self.updateGUI()
    
        def testSample(self):
            # Do whatever you need to do to test your sample
            pass
    
        def beginTest(self, *args):   # This is called when the user clicks the OptionMenu to begin the test
            self.testing = True
    
            sf = "IC Network Analyzer"
            self.master.title(sf)
    
            self.testOption.config(state=tk.DISABLED)
            self.okButton.config(state=tk.NORMAL)
            self.okLabel.config(text="Ready first sample, then press OK")
            self.updateGUI()
    
        def nextSample(self):         # This is called each time a new sample is tested.
            if self.sampleNumber >= self.maxSamples:  # If the maximum # of samples has been reached, end the test sequence
                self.testing = False
                self.sampleNumber = 1
                self.testOption.config(state=tk.NORMAL)
                self.okButton.config(state=tk.DISABLED)
    
                # You'll want to save your sample data to a file or something here
    
                self.samples = []
    
                self.updateGUI()
            else:
                self.sampleNumber += 1
                if self.var.get() == "TEST":
                    self.samples.append(self.WILTRON.Sample_Data())
                    self.okLabel.config(text="Switch to sample #"+str(self.sampleNumber)+" and press OK")
                    #At this point, the GUI will wait politely for you to push the okButton, before it runs this method again.
                    #ect.
                #####
    
        def updateGUI(self):
            self.frame.grid()
            self.testOption.grid(row=1, column=1)
            if self.testing:
                self.okLabel.grid(row=2, column=1)
                self.okButton.grid(row=3, column=1)
            else:
                self.okLabel.grid_remove()
                self.okButton.grid_remove()
            self.master.update_idletasks()
    
    root = tk.Tk()
    a = App(root)
    root.mainloop()
    

相关问题