在python tkinter中暂停事件



我在树莓派上使用python 2.7 tkinter gui来自动化一些材料测试。对于测试,必须测试多个样本,并且需要时间来交换样本。我想提示文字说的东西,如"请插入样本一个然后按键盘上的enter键",并有功能暂停,直到进入已按下。除了按回车键,我还可以使用一个点击按钮。有没有不使用外部库的想法?我已经尝试了一个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

下面是一个工作示例,它使用回调来启动测试,并使用回调来推进每个示例。

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

使用tkMessageBox

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

相关内容

  • 没有找到相关文章

最新更新