I’m using Python 3.6, OpenCV3, and PyQT5 to write a specialized video player in Windows. However, the playback is slow when playing a video file that was recorded at 30.0 frames per second (recorded off my laptop webcam).
I stripped my application to the barest part for playing video. I then varied the playback loop in my application from 15 milliseconds per frame to 34 milliseconds per frame and recorded each loop time. Next, I computed the average loop time for 232 frames of playback at each loop speed. The results are puzzling. The application behaved as expected for loops of 15 ms through 19ms, but then was a steady 31.2 ms when the application specified the loop be at 20 ms through 31 ms. Then the loop time jumped again when the application specified loops slower than 31 msec.
Application = 15msec, Average Result = 15.0msec, Difference = 0.0msec
Application = 16msec, Average Result = 16.0msec, Difference = 0.0msec
Application = 17msec, Average Result = 17.0msec, Difference = 0.0msec
Application = 18msec, Average Result = 18.0msec, Difference = 0.0msec
Application = 19msec, Average Result = 19.0msec, Difference = 0.0msec
Application = 20msec, Average Result = 31.2msec, Difference = 11.2msec
Application = 21msec, Average Result = 31.2msec, Difference = 10.2msec
Application = 22msec, Average Result = 31.2msec, Difference = 9.2msec
Application = 23msec, Average Result = 31.2msec, Difference = 8.2msec
Application = 24msec, Average Result = 31.2msec, Difference = 7.2msec
Application = 25msec, Average Result = 31.2msec, Difference = 6.2msec
Application = 26msec, Average Result = 31.2msec, Difference = 5.2msec
Application = 27msec, Average Result = 31.2msec, Difference = 4.2msec
Application = 28msec, Average Result = 31.2msec, Difference = 3.2msec
Application = 29msec, Average Result = 31.2msec, Difference = 2.2msec
Application = 30msec, Average Result = 31.2msec, Difference = 1.2msec
Application = 31msec, Average Result = 31.2msec, Difference = 0.2msec
Application = 32msec, Average Result = 39.1msec, Difference = 7.1msec
Application = 33msec, Average Result = 46.8msec, Difference = 13.8msec
Application = 34msec, Average Result = 46.8msec, Difference = 12.8msec
I also timed how long it takes to execute the nextFrameSlot(self) method. It takes an average of 6msec to execute, so it should not induce any delay to the loops.
I want the playback speed to be at the true rate, which should be 1/(frame rate).
Does anyone have any suggestions why this is happening? Here is the code. (I didn’t include the GUI code that pydesigner created).
from PyQt5.QtWidgets import QApplication, QMainWindow
from PyQt5.QtGui import QImage, QPixmap
from PyQt5.QtCore import QTimer
from time import time
import sys
import cv2
import vidtest # GUI Module created by pydesigner
class VideoCapture(QMainWindow, vidtest.Ui_MainWindow):
def __init__(self):
super(self.__class__, self).__init__()
self.setupUi(self) # This is defined in design.py file automatically
self.btnLoadFile.clicked.connect(self.loadVideoFile)
self.btnStartPlay.clicked.connect(self.start)
self.btnStop.clicked.connect(self.closeApplication)
self.durations = []
def nextFrameSlot(self):
ret, frame = self.cap.read()
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
x = frame.shape[1]
y = frame.shape[0]
img = QImage(frame, x, y, QImage.Format_RGB888)
pix = QPixmap.fromImage(img)
self.vidWindow.setPixmap(pix)
a = time() # Used to record loop time
self.durations.append(a) # Used to record loop time
def start(self):
self.timer = QTimer()
print("Rate = ", self.vid_rate)
self.timer.timeout.connect(self.nextFrameSlot)
self.timer.start(self.vid_rate)
def loadVideoFile(self):
self.videoFileName = "stopwatch.avi"
self.cap = cv2.VideoCapture(str(self.videoFileName))
self.frame_rate = self.cap.get(cv2.CAP_PROP_FPS)
self.vid_rate = 34 # reset this integer from 15 through 34
self.nextFrameSlot()
def closeApplication(self):
for i in self.durations:
print (i)
self.cap.release()
sys.exit(0)
def main():
app = QApplication(sys.argv)
form = VideoCapture()
form.show()
app.exec_()
if __name__ == '__main__':
main()
Thanks, user3419537! Your suggestion worked.
I needed to specify self.timer.setTimerType(Qt.PreciseTimer) after the statement self.timer = QTimer() in the function start(self). By default, QTimer() uses a coarse timer.
Related
I'd like to be able to run the exact same run, given I didn't change any parameters of the simulation, in the autonomous driving simulator Carla. Before I paste my code, my logic is that I have to set a specific seed for any Random operation to be repeatable, set a specific seed for the traffic manager, and in general to work in synchronous_mode=True so the lag in my computer won't interrupt(?). As you'll see, I log the x,y,z location of the ego vehicle, and run the simulation twice. It is similar, but not the same. What can I do to make it repeatable (not in recording mode, actual live runs)?
Additional info: Carla 0.9.14 on Ubuntu 20.04, Python 3.8.
import random
import numpy as np
import sys
import os
try:
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))) + '/carla')
except IndexError:
pass
import carla
from agents.navigation.behavior_agent import BehaviorAgent # pylint: disable=import-error
seed = 123
N_vehicles = 50
camera = None
telemetry = []
random.seed(seed)
try:
# Connect the client and set up bp library and spawn points
client = carla.Client('localhost', 2000)
client.set_timeout(60.0)
world = client.get_world()
bp_lib = world.get_blueprint_library()
spawn_points = world.get_map().get_spawn_points()
settings = world.get_settings()
settings.synchronous_mode = True
settings.fixed_delta_seconds = 0.10
world.apply_settings(settings)
traffic_manager = client.get_trafficmanager()
traffic_manager.set_random_device_seed(seed)
traffic_manager.set_synchronous_mode(True)
# Spawn ego vehicle
vehicle_bp = bp_lib.find('vehicle.audi.a2')
# breakpoint()
vehicle = world.try_spawn_actor(vehicle_bp, random.choice(spawn_points))
# Move spectator behind vehicle to motion
spectator = world.get_spectator()
transform = carla.Transform(vehicle.get_transform().transform(carla.Location(x=-6,z=2.5)),vehicle.get_transform().rotation)
spectator.set_transform(transform)
world.tick()
# set the car's controls
agent = BehaviorAgent(vehicle, behavior="normal")
destination = random.choice(spawn_points).location
agent.set_destination(destination)
print('destination:')
print(destination)
print('current location:')
print(vehicle.get_location())
#Iterate this cell to find desired camera location
camera_bp = bp_lib.find('sensor.camera.rgb')
# Spawn camera
camera_init_trans = carla.Transform(carla.Location(z=2))
camera = world.spawn_actor(camera_bp, camera_init_trans, attach_to=vehicle)
# Callback stores sensor data in a dictionary for use outside callback
def camera_callback(image, data_dict):
data_dict['image'] = np.reshape(np.copy(image.raw_data), (image.height, image.width, 4))
# Get gamera dimensions and initialise dictionary
image_w = camera_bp.get_attribute("image_size_x").as_int()
image_h = camera_bp.get_attribute("image_size_y").as_int()
camera_data = {'image': np.zeros((image_h, image_w, 4))}
# Start camera recording
camera.listen(lambda image: camera_callback(image, camera_data))
# Add traffic to the simulation
SpawnActor = carla.command.SpawnActor
SetAutopilot = carla.command.SetAutopilot
FutureActor = carla.command.FutureActor
vehicles_list, batch = [], []
for i in range(N_vehicles):
ovehicle_bp = random.choice(bp_lib.filter('vehicle'))
npc = world.try_spawn_actor(ovehicle_bp, random.choice(spawn_points))
# add it if it was successful
if(npc):
vehicles_list.append(npc)
print(f'only {len(vehicles_list)} cars were spawned')
world.tick()
# Set the all vehicles in motion using the Traffic Manager
for idx, v in enumerate(vehicles_list):
try:
v.set_autopilot(True)
except:
pass
# Game loop
while True:
world.tick()
pose = vehicle.get_location()
telemetry.append([pose.x, pose.y, pose.z])
# keep following the car
transform = carla.Transform(vehicle.get_transform().transform(carla.Location(x=-6,z=2.5)),vehicle.get_transform().rotation)
spectator.set_transform(transform)
if agent.done():
print("The target has been reached, stopping the simulation")
break
control = agent.run_step()
control.manual_gear_shift = False
vehicle.apply_control(control)
finally:
# Stop the camera when we've recorded enough data
if(camera):
camera.stop()
camera.destroy()
settings = world.get_settings()
settings.synchronous_mode = False
settings.fixed_delta_seconds = None
world.apply_settings(settings)
traffic_manager.set_synchronous_mode(True)
if(vehicles_list):
client.apply_batch([carla.command.DestroyActor(v) for v in vehicles_list])
vehicle.destroy()
np.savetxt('telemetry.txt', np.array(telemetry), delimiter=',')
y-axis is the error between two runs, x-axis is the time index of the run
I have a Process pool in python that is starting processes as normal, however, I have just realized that these processes are not closed after the completion (I know that they completed as the last statement is a file write).
Below the code, with an example function ppp:
from multiprocessing import Pool
import itertools
def ppp(element):
window,day = element
print(window,day)
time.sleep(10)
if __name__ == '__main__': ##The line marked
print('START')
start_time = current_milli_time()
days = ['0808', '0810', '0812', '0813', '0814', '0817', '0818', '0827']
windows = [1000,2000,3000,4000,5000,10000,15000, 20000,30000,60000,120000,180000]
processes_args = list(itertools.product(windows, days))
pool = Pool(8)
results = pool.map(ppp, processes_args)
pool.close()
pool.join()
print('END', current_milli_time()-start_time)
I am working on Linux, Ubuntu 16.04. Everything was working fine before I added the line marked in the example. I am wondering if that behavior can be related to the missing of a return statement. Anyway, that is what looks like my 'htop':
As you can see, no process is closed, but all have completed their work.
I found that related question: Python Multiprocessing pool.close() and join() does not close processes, however, I have not understood if the solution to this problem is to use map_async instead of map.
EDIT: real function code:
def process_day(element):
window,day = element
noise = 0.2
print('Processing day:', day,', window:', window)
individual_files = glob.glob('datan/'+day+'/*[0-9].csv')
individual = readDataset(individual_files)
label_time = individual.loc[(individual['LABEL_O'] != -2) | (individual['LABEL_F'] != -2), 'TIME']
label_time = list(np.unique(list(label_time)))
individual = individual[individual['TIME'].isin(label_time)]
#Saving IDs for further processing
individual['ID'] = individual['COLLAR']
#Time variable in seconds for aggregation and merging
individual['TIME_S'] = individual['TIME'].copy()
noise_x = np.random.normal(0,noise,len(individual))
noise_y = np.random.normal(0,noise,len(individual))
noise_z = np.random.normal(0,noise,len(individual))
individual['X_AXIS'] = individual['X_AXIS'] + noise_x
individual['Y_AXIS'] = individual['Y_AXIS'] + noise_y
individual['Z_AXIS'] = individual['Z_AXIS'] + noise_z
#Time syncronization (applying milliseconds for time series processing)
print('Time syncronization:')
with progressbar.ProgressBar(max_value=len(individual.groupby('ID'))) as bar:
for baboon,df_baboon in individual.groupby('ID'):
times = list(df_baboon['TIME'].values)
d = Counter(times)
result = []
for timestamp in np.unique(times):
for i in range(0,d[timestamp]):
result.append(str(timestamp+i*1000/d[timestamp]))
individual.loc[individual['ID'] == baboon,'TIME'] = result
bar.update(1)
#Time series process
ts_process = time_series_processing(window, 'TIME_S', individual, 'COLLAR', ['COLLAR', 'TIME', 'X_AXIS','Y_AXIS','Z_AXIS'])
#Aggregation and tsfresh
ts_process.do_process()
individual = ts_process.get_processed_dataframe()
individual.to_csv('noise2/processed_data/'+str(window)+'/agg/'+str(day)+'.csv', index = False)
#NEtwork inference process
ni = network_inference_process(individual, 'TIME_S_mean')
#Inference
ni.do_process()
final = ni.get_processed_dataframe()
final.to_csv('noise2/processed_data/'+str(window)+'/net/'+str(day)+'.csv', index = False)
#Saving not aggregated ground truth
ground_truth = final[['ID_mean', 'TIME_S_mean', 'LABEL_O_values', 'LABEL_F_values']].copy()
#Neighbor features process
neighbors_features_f = ni.get_neighbor_features(final, 'TIME_S_mean', 'ID_mean')
neighbors_features_f = neighbors_features_f.drop(['LABEL_O_values_n', 'LABEL_F_values_n'], axis=1)
neighbors_features_f.to_csv('noise2/processed_data/'+str(window)+'/net/'+str(day)+'_neigh.csv', index = False)
# Final features dataframe
final_neigh = pd.merge(final, neighbors_features_f, how='left', left_on=['TIME_S_mean','ID_mean'], right_on = ['TIME_S_mean_n','BABOON_NODE_n'])
final_neigh.to_csv('noise2/processed_data/'+str(window)+'/complete/'+str(day)+'.csv', index = False)
return
So as you can see, the last statement is a write to file, and it is executed by all the processes, I do not actually think that the problem is inside this function.
#!/usr/bin/env python
import sys
import vtk
from PyQt4 import QtCore, QtGui
from vtk.qt4.QVTKRenderWindowInteractor import QVTKRenderWindowInteractor
import pickle
import numpy as np
from time import time
import matplotlib.pyplot as plt
import scipy as sp
class MainWindow(QtGui.QMainWindow):
def __init__(self, parent = None):
QtGui.QMainWindow.__init__(self, parent)
self.frame = QtGui.QFrame()
self.vl = QtGui.QVBoxLayout()
self.vtkWidget = QVTKRenderWindowInteractor(self.frame)
self.vl.addWidget(self.vtkWidget)
self.ren = vtk.vtkRenderer()
self.renWin= self.vtkWidget.GetRenderWindow()
self.renWin.AddRenderer(self.ren)
self.iren = self.renWin.GetInteractor()
t0 = time()
try:
with open('/home/nxpd/VTK/Image35.pickle','rb') as readData:
I = pickle.load(readData)
except pickle.PickleError as pErr:
print('Pickling error: '+ str(pErr))
t1 = time()
print "Time elapsed in pickle reading: " , t1 - t0
data_matrix = I
#print data_matrix
# For VTK to be able to use the data, it must be stored as a VTK-image. This can be done by the vtkImageImport-class which
# imports raw data and stores it.
dataImporter = vtk.vtkImageData()
dataImporter.SetDimensions(728,728,35)
data=vtk.vtkUnsignedCharArray()
data.SetName('scalar')
#data.SetNumberOfTuples(728*728*35)
for k in range(35):
for j in range(728):
for i in range(728):
data.InsertNextTuple1(data_matrix[i][j][k]/256)
t2 = time()
print "Time elapsed in writing to array: " , t2 - t1
dataImporter.SetExtent(0, 727, 0,727,0,34)
# The following class is used to store transparencyv-values for later retrieval. In our case, we want the value 0 to be
# completly opaque whereas the three different cubes are given different transperancy-values to show how it works.
alphaChannelFunc = vtk.vtkPiecewiseFunction()
alphaChannelFunc.AddPoint(0, 0)
alphaChannelFunc.AddPoint(53000/256, 0)
alphaChannelFunc.AddPoint(53000/256+1, 1)
alphaChannelFunc.AddPoint(65535/256, 1)
# This class stores color data and can create color tables from a few color points. For this demo, we want the three cubes
# to be of the colors red green and blue.
colorFunc = vtk.vtkColorTransferFunction()
colorFunc.AddRGBPoint(0, 0, 0, 0)
colorFunc.AddRGBPoint(53000/256, 0,0,0)
colorFunc.AddRGBPoint(53000/256+1, 1,1,1)
colorFunc.AddRGBPoint(65535/256, 1, 1, 1)
# The preavius two classes stored properties. Because we want to apply these properties to the volume we want to render,
# we have to store them in a class that stores volume prpoperties.
volumeProperty = vtk.vtkVolumeProperty()
volumeProperty.SetColor(colorFunc)
volumeProperty.SetScalarOpacity(alphaChannelFunc)
# This class describes how the volume is rendered (through ray tracing).
compositeFunction = vtk.vtkVolumeRayCastCompositeFunction()
# We can finally create our volume. We also have to specify the data for it, as well as how the data will be rendered.
volumeMapper = vtk.vtkVolumeRayCastMapper()
volumeMapper.SetVolumeRayCastFunction(compositeFunction)
volumeMapper.SetInputData(dataImporter)
# The class vtkVolume is used to pair the preaviusly declared volume as well as the properties to be used when rendering that volume.
volume = vtk.vtkVolume()
volume.SetMapper(volumeMapper)
volume.SetProperty(volumeProperty)
t3 = time()
print "Time elapsed in Setting parameters: " , t3 - t2
# With almost everything else ready, its time to initialize the renderer and window, as well as creating a method for exiting the application
#renderer = vtk.vtkRenderer()
#renderWin = vtk.vtkRenderWindow()
#renderWin.AddRenderer(renderer)
#renderInteractor = vtk.vtkRenderWindowInteractor()
#renderInteractor.SetRenderWindow(renderWin)
# We add the volume to the renderer ...
self.ren.AddVolume(volume)
# ... set background color to white ...
self.ren.SetBackground(0.5,0.5,0.7)
# ... and set window size.
self.renWin.SetSize(800,800)
t4 = time()
print "Time elapsed in rendering: " , t4 - t3
# Tell the application to use the function as an exit check.
self.renWin.AddObserver("AbortCheckEvent", exitCheck)
self.iren.Initialize()
# Because nothing will be rendered without any input, we order the first render manually before control is handed over to the main-loop.
self.ren.Render()
self.ren.ResetCamera()
self.iren.Start()
t5 = time()
print "Time elapsed in setting up: " , t5 - t4
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
window = MainWindow()
sys.exit(app.exec_())
I am not able to figure out why this code is showing me segmentation fault. I have used the example from http://www.vtk.org/Wiki/VTK/Examples/Python/Widgets/EmbedPyQt to get the details how exactly it works.
The standalone code works fine without using pyqt.
I'm using TKinter to draw a GUI for a python program im making, and I have it updating at about 200ms, but when the program queries the data it locks the program because it takes a second to get the data. I tried to write it into multi processing so each query would be its own process and just share the info with global variables because my program is a real time program that uses wmi to get performance data. At least thats what I have so far. Not the end goal just the start. So if you could help me figure out why even with multiprocessing if it queries the info while I'm dragging the app across the screen it will freeze for a second.
import wmi
import time
import Tkinter as tk
from multiprocessing import cpu_count
import Image
from PIL import ImageTk
from Tkinter import Button, Label
import threading
from multiprocessing import Process, Value, Array
window = Tk();
global pct_in_use
global available_mbytes
global utilization
global hours_up
a= 0
b=0
def build_labels(gui, string):
var = StringVar()
label = Label( gui, textvariable=var, relief=RAISED )
var.set(string)
return label
def get_uptime():
global hours_up
c = wmi.WMI()
secs_up = int([uptime.SystemUpTime for uptime in c.Win32_PerfFormattedData_PerfOS_System()][0])
hours_up = secs_up / 3600
return hours_up
def get_cpu():
global utilization
c = wmi.WMI()
utilizations = [cpu.LoadPercentage for cpu in c.Win32_Processor()]
utilization = int(sum(utilizations) / len(utilizations)) # avg all cores/processors
return utilization
def get_mem_mbytes():
global available_mbytes
c = wmi.WMI()
available_mbytes = int([mem.AvailableMBytes for mem in c.Win32_PerfFormattedData_PerfOS_Memory()][0])
return available_mbytes
def get_mem_pct():
global pct_in_use
c = wmi.WMI()
pct_in_use = int([mem.PercentCommittedBytesInUse for mem in c.Win32_PerfFormattedData_PerfOS_Memory()][0])
return pct_in_use
def Draw():
global mem_per_lb
global cpu_lb
global up_time_lb
global mb_used_lb
mem_pct = 0
mem_per_lb = tk.Label(text='Memory % ' + str(mem_pct))
mem_per_lb.place(x=10, y=10)
cpu = 0
cpu_lb = tk.Label(text='CPU % ' + str(cpu))
cpu_lb.place(x=10, y=30)
mem_pct = 0
up_time_lb = tk.Label(text='UP Time % ' + str(mem_pct))
up_time_lb.place(x=10, y=50)
mem_pct = 0
mb_used_lb = tk.Label(text='Memory MB ' + str(mem_pct))
mb_used_lb.place(x=10, y=70)
def Refresher():
global mem_per_lb
global cpu_lb
global up_time_lb
global mb_used_lb
mem_pct = get_mem_pct()
cpu = get_cpu()
up_time = get_uptime()
mbused = get_mem_mbytes()
window.wm_title('Vision' + time.asctime())
mem_per_lb.configure(text='Memory % ' + str(pct_in_use))
cpu_lb.configure(text='CPU ' + str(utilization))
up_time_lb.configure(text='UP Time ' + str(hours_up))
mb_used_lb.configure(text='Memory MB ' + str(available_mbytes))
window.after(200, Refresher) # every second...
def draw_window(): #creates a window
window.geometry('704x528+100+100')
image = Image.open('bg.jpg') #gets image (also changes image size)
image = image.resize((704, 528))
imageFinal = ImageTk.PhotoImage(image)
label = Label(window, image = imageFinal) #creates label for image on window
label.pack()
label.place(x = a, y = b) #sets location of label/image using variables 'a' and 'b'
Draw()
Refresher()
window.mainloop()
up_time_p = Process(target=get_uptime())
cpu_p = Process(target=get_cpu())
mb_p = Process(target=get_mem_mbytes())
pct_p = Process(target=get_mem_pct())
win_p = Process(target=draw_window())
up_time_p.start()
mb_p.start()
pct_p.start()
cpu_p.start()
win_p.start()
up_time_p = Process(target=get_uptime())
cpu_p = Process(target=get_cpu())
mb_p = Process(target=get_mem_mbytes())
pct_p = Process(target=get_mem_pct())
win_p = Process(target=draw_window())
I don't think you're supposed to include parentheses when you supply targets to a process. If you do that, the functions will execute in the main thread, and whatever those functions return will become the target.
up_time_p = Process(target=get_uptime)
cpu_p = Process(target=get_cpu)
mb_p = Process(target=get_mem_mbytes)
pct_p = Process(target=get_mem_pct)
win_p = Process(target=draw_window)
As per Kevin's answer, you're calling the functions when you create each process instance. So they are all actually running in the main process.
However, once you fix that problem your 'global' variables aren't going to work as you expect. When a process is created it takes a COPY of the parent processes memory. Any changes to that memory are not shared between the processes.
To achieve the result you want you'll have to use Python's threading library. Not the multiprocess library.
Threads share the same memory space as the parent process. Which can lead to its own problems. Though in your case the global variables you're changing are just integer constants so it should be okay.
from threading import Thread
data_funcs = (
get_uptime,
get_cpu,
get_mem_mbytes,
get_mem_pct,
draw_window
)
threads = [Thread(target=f) for f in data_funcs]
for t in threads:
t.start()
Is the general pattern you should use. You'll then have to figure out a way of killing those threads when you shut down the main process or it will hang.
I've got a program that will eventually receive data from an external source over serial, but I'm trying to develop the display-side first.
I've got this "main" module that has the simulated data send and receive. It updates a global that is used by a Matplotlib stripchart. All of this works.
#-------------------------------------------------------------------------------
# Name: BBQData
# Purpose: Gets the data from the Arduino, and runs the threads.
#-------------------------------------------------------------------------------
import time
import math
import random
from threading import Thread
import my_globals as bbq
import sys
import BBQStripChart as sc
import serial
import BBQControl as control
ser = serial.serial_for_url('loop://', timeout=10)
def simData():
newTime = time.time()
if not hasattr(simData, "lastUpdate"):
simData.lastUpdate = newTime # it doesn't exist yet, so initialize it
simData.firstTime = newTime # it doesn't exist yet, so initialize it
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 and testCtr<100:
newData = simData()
if newData != None:
reportStr = "D " + "".join(['{:3.0f} ' for 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]
for 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 like to add a SEPARATE tkinter window that just has the most recent data on it, a close button, etc. I can get that window to come up, and show data initially, but none of the other threads run. (and nothing works when I try to run the window and the plot at the same time.)
#-------------------------------------------------------------------------------
# Name: BBQ Display/Control
# Purpose: displays current temp data, and control options
#-------------------------------------------------------------------------------
import tkinter as tk
import tkinter.font
import my_globals as 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 to 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()
Your title sums up the problem nicely: Tkinter doesn't play well with threads. That's not a question, that's the answer.
You can only access tkinter widgets from the same thread that created the widgets. If you want to use threads, you'll need your non-gui threads to put data on a queue and have the gui thread poll the queue periodically.
One way of getting tkinter to play well with threads is to modify the library so all method calls run on a single thread. Two other questions deal with this same problem: Updating a TKinter GUI from a multiprocessing calculation and Python GUI is not responding while thread is executing. In turn, the given answers point to several modules that help to solve the problem you are facing. Whenever I work with tkinter, I always use the safetkinter module in case threads appear to be helpful in the program.