1

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

    win.quit()
menubar = Menu(win)


win.title("Measurment v1.0")
win.geometry('800x480')
win.configure(background='#CD5C5C')
#Labels
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)
win.mainloop()

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

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

    cb=[]
    tally=[0]*NUM_GPIO
    last=[0]*NUM_GPIO

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

def mainFunction(self):

    output_value = self.measure()
    return output_value

Object1= mainClass()
Object1.mainFunction()
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 :) – sunny_old_days Jul 1 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.