python - Tkinter window not playing well with threads -
i've got program receive data external source on serial, i'm trying develop display-side first.
i've got "main" module has simulated data send , receive. updates global used matplotlib stripchart. of works.
#------------------------------------------------------------------------------- # name: bbqdata # purpose: gets data arduino, , runs threads. #------------------------------------------------------------------------------- import time import math import random threading import thread import my_globals bbq import sys import bbqstripchart sc import serial import bbqcontrol control ser = serial.serial_for_url('loop://', timeout=10) def simdata(): newtime = time.time() if not hasattr(simdata, "lastupdate"): simdata.lastupdate = newtime # doesn't exist yet, initialize simdata.firsttime = newtime # doesn't exist yet, initialize if newtime > simdata.lastupdate: simdata.lastupdate = newtime return (140 + 0.05*(simdata.lastupdate - simdata.firsttime), \ 145 + 0.022*(simdata.lastupdate - simdata.firsttime), \ 210 + random.randrange(-10, 10)) else: return none def serialdatapump(): testctr = 0; while not bbq.closing , testctr<100: newdata = simdata() if newdata != none: reportstr = "d " + "".join(['{:3.0f} ' x in newdata]) + '\n' reportstr = reportstr.format(*newdata) ser.write(bytes(reportstr, 'ascii')) testctr+=1 time.sleep(1) bbq.closing = true def serialdatarcv(): while not bbq.closing: line = ser.readline() rcvdtime = time.time() temps = str(line, 'ascii').split(" ") temps = temps[1:-1] j, x in enumerate(temps): bbq.temps[j].append(float(x)) bbq.plottimes.append(rcvdtime) def main(): sendthread = thread(target = serialdatapump) receivethread = thread(target = serialdatarcv) sendthread.start() receivethread.start() # sc.runui() control.runcontrol() #blocks until user closes window bbq.closing = true time.sleep(2) exit() if __name__ == '__main__': main() ## testsermain()
however, i'd add separate tkinter window has recent data on it, close button, etc. can window come up, , show data initially, none of other threads run. (and nothing works when try run window , plot @ same time.)
#------------------------------------------------------------------------------- # name: bbq display/control # purpose: displays current temp data, , control options #------------------------------------------------------------------------------- import tkinter tk import tkinter.font import my_globals bbq import threading fontsize = 78 class bbqcontrol(tk.tk): def __init__(self,parent): tk.tk.__init__(self,parent) self.parent = parent self.labelfont = tkinter.font.font(family='helvetica', size=int(fontsize*0.8)) self.datafont = tkinter.font.font(family='helvetica', size=fontsize, weight = 'bold') self.makewindow() def makewindow(self): self.grid() btnclose = tk.button(self,text=u"close") btnclose.grid(column=1,row=5) lblfood = tk.label(self,anchor=tk.center, text="food temps", \ font = self.labelfont) lblfood.grid(column=0,row=0) lblpit = tk.label(self,anchor=tk.center, text="pit temps", \ font = self.labelfont) lblpit.grid(column=1,row=0) self.food1temp = tk.stringvar() lblfoodtemp1 = tk.label(self,anchor=tk.e, \ textvariable=self.food1temp, font = self.datafont) lblfoodtemp1.grid(column=0,row=1) #spawn thread update temps updatethread = threading.thread(target = self.updateloop) updatethread.start() def updateloop(self): self.food1temp.set(str(bbq.temps[1][-1])) def runcontrol(): app = bbqcontrol(none) app.title('bbq display') app.after(0, app.updateloop) app.mainloop() bbq.closing = true if __name__ == '__main__': runcontrol()
any thoughts appreciated.
your title sums problem nicely: tkinter doesn't play threads. that's not question, that's answer.
you can access tkinter widgets same thread created widgets. if want use threads, you'll need non-gui threads put data on queue , have gui thread poll queue periodically.
Comments
Post a Comment