0

I am using a python 2.7 tkinter gui on a raspberry pi to automate some material testing. For the testing, multiple samples must be tested and it takes time to swap out the samples. I want to prompt text saying something like "Please insert sample one then press enter on keyboard" and have the function pause until enter has been pressed. Instead of pressing enter I could also use a tkinter button. Any ideas without using external libraries? I have tried a while loop in which I try and exit the loop once a button is pressed, but since the loop is running the button does not register.

Sample code (removed lots of code and left what is relevant):

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
  • There are dozens of ways to do this. If you can post some code, perhaps the "sample testing" function, it will be easier to advise you. For example - are you using classes to create your GUI, or some kind of global variable system? Commented Jun 19, 2014 at 22:09
  • Hey @Brionius, I have edited my original post to include the relevant code. Thank you for the reply! Commented Jun 19, 2014 at 22:32

2 Answers 2

2

Here's a working example that uses a callback to start the test, and a callback to advance each sample.

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()
Sign up to request clarification or add additional context in comments.

Comments

1

Use tkMessageBox

import Tkinter
import tkMessageBox

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

1 Comment

Awesome, thank you! I like that solution even more than what I was picturing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.