0

I am beginner to python and stucked with something in tkinter. Below is the code that application receives GPS data from my bluetooth module and I want to output "data" to my Text widget "outputtext".

import serial
from tkinter import *

root = Tk()

serialPort = serial.Serial(port='COM8', baudrate=9600, timeout=0, parity=serial.PARITY_EVEN, stopbits=1)
size = 10240


def myClick():
    while serialPort:
        data = serialPort.readline(size)

        if data:
            print(data)
            outputtext.insert('1.0', data)


outputtext = Text(root)
outputtext.pack()

myButton = Button(root, text="Retrieve Bluetooth Data!", command=myClick)
myButton.pack()
root.mainloop()

print is working perfectly however I am not able to output "data" to "outputtext". Application does not respond immediately after clicking "myButton". This might be due to huge data I receive (NMEA sentence every second). Maybe I should use buffer or something. Any help would be highly appreciated.

Ali Ülkü
  • 86
  • 1
  • 9

1 Answers1

0

In order to do that, we will need a threading. Changing below line

myButton = Button(root, text="Retrieve Bluetooth Data!", command=myClick)

with

myButton = Button(root, text="Retrieve Bluetooth Data!", command=threading.Thread(target=myClick).start)

will allow you to run myClick() function parallel to other functions

Ali Ülkü
  • 86
  • 1
  • 9