Rob M
Rob M

Reputation: 1

Terminal print to tkinter GUI Label

I am currently trying to generate an output to a gui from a program that prints a value to the terminal and I cannot figure out how.

The program that is generating the output is as follows:

import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)
input_A = 18
input_B = 23
GPIO.setup(input_A, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(input_B, GPIO.IN, pull_up_down=GPIO.PUD_UP)
old_a = True
old_b = True

def get_encoder_turn():
     # return -1, 0, or +1
     global old_a, old_b
     result = 0
     new_a = GPIO.input(input_A)
     new_b = GPIO.input(input_B)
     if new_a != old_a or new_b != old_b :
         if old_a == 0 and new_a == 1 :
            result = (old_b * 2 - 1)
     elif old_b == 0 and new_b == 1 :
         result = -(old_a * 2 - 1)
     old_a, old_b = new_a, new_b
     time.sleep(0.001)
     return result
x = 0
while True:
     change = get_encoder_turn()
     if change != 0 :
        x = x + change
        print(x) 

This program will output numbers to the terminal until the program is exited.

Now the part I am struggling with is getting the output x to appear in a tkinter GUI. Can you invoke the program as a sub process and intercept the outputs? Is there another way in which I can update a label in a GUI with the current value of x?

Upvotes: 0

Views: 403

Answers (1)

Bryan Oakley
Bryan Oakley

Reputation: 386342

You can write a tkinter program that imports this module. You can then call get_encoder_turn to calculate a value. To have it run continuously, use the tkinter after method on the root window to call the function every X milliseconds.

There are several examples on this site of doing this sort of loop using after.

Upvotes: 1

Related Questions