I am developing a GUI (with python tkinter) application for a measurement system with Raspberry pi. I made the GUI and the code to do the measurement. But my question is how to link them together. There is a start button in my application. when someone presses it, the measurement should start and pressing the stop button should stop the measurement. measured result should show in two labels named MeasurementUnit1Values and MeasurementUnit2Values. Any suggestions. When i am running, measurement is starting only when I closed the GUI

import time
import numpy as np
import pigpio

#main Window using Tk
win = Tk()
myFont = tkinter.font.Font(family = 'Verdana', size = 20, weight = 'bold')
#function to close the window
def exitProgram():

menubar = Menu(win)

win.title("Measurment v1.0")
startButton  = Button(win, text = "Start", height =1 , width = 10)
startButton.place(x=15, y=20)
stopButton  = Button(win, text = "Stop", height =1 , width = 10)
stopButton.place(x=200, y=20)

#display values
MeasurementUnit1Values = Label(win, text = "value", height =2, width = 12)
MeasurementUnit1Values.place(x=200, y=100)

MeasurementUnit2Values = Label(win, text = "value", height =2, width = 12)
MeasurementUnit2Values.place(x=200, y=200)

exitButton  = Button(win, text = "Exit",fg='white',   bg='#008B8B', font=('Verdana', 14), command = exitProgram, height =1 , width = 14)
exitButton.place(x=600, y=400)

class mainClass():
def measure(self):
    pigpio.exceptions = True
    GPIO=[23, 24]  #raspberry pi pins

#NUM_GPIO saves the length of the list GPIO.#
#-------cb is a list to save each values measured by the detector with the help of callback function----#


#pin as input pins for measurement
    for g in GPIO:
        pi.set_mode(g, pigpio.INPUT)
    while True:
# a list to save the measured values
        mesurements = []
        for i in range(NUM_GPIO):
            tally[i] = cb[i].tally()
            mesurements.append((tally[i]-last[i]) - arg[i])
            last[i] = tally[i]

def mainFunction(self):

    output_value = self.measure()
    return output_value

Object1= mainClass()

1 Answer 1


Don't use while True in a tkinter program. Instead move the taking a measurement code into a function and drop the time.sleep call. Then use the tkinter after method to schedule a call to your measurement method once a second (eg: win.after(1000, self.take_measurement)

Tkinter MUST process events promptly to handle drawing on screen, mouse and keyboard input and so on. You use after to add timer events into the event sequence. Use after_cancel to cancel events if necessary.

  • thank you.. I will try and get back with the result :) Jul 1, 2019 at 11:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.