在python tkin中暂停事件

2024-05-12 19:09:33 发布

您现在位置:Python中文网/ 问答频道 /正文

我在raspberry pi上使用Python2.7TkinterGUI来自动化一些材料测试。为了进行测试,必须测试多个样本,更换样本需要时间。我想要提示文本,比如“请插入示例一,然后按键盘上的enter键”,并让函数暂停,直到按下enter键。我也可以用一个tkinter按钮来代替按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

Tags: theto代码selfapp示例vardef
2条回答

使用tkMessageBox

import Tkinter
import tkMessageBox

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

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

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

相关问题 更多 >