I have made a python code & using multithreading in it. this is very basic
code, not using queues & other stuff.
1. Task is to create two independent Tkinter windows
2. Code has four files:
main.py:creates individual thread
analog.py: generate random values every 1 sec & 10 sec
screen.py: Tkinter file, screen keeps on updating every 1 sec
scree2.py: Tkinter file, screen keeps on updating every 10 sec
3. My code never run on gives anything. However if in main.py, I disable, one
of either screen thread, another screen works fine. i.e they don't work
together
main.py
[code]import thread
import time
import analog
import screen
import screen2
thread.start_new_thread(analog.get_analog_1 , ("Thread-1",))
thread.start_new_thread(analog.get_analog_2 , ("Thread-2",))
thread.start_new_thread(screen.display , ("Thread-3",))
thread.start_new_thread(screen2.display , ("Thread-4",))
while 1:
pass
[/code]
analog.py
[code]import time
from random import randint
current_time_1 = time.time();
current_time_2 = time.time();
read_ok_1 = 0
read_ok_2 = 0
analog_1 = 0
analog_2 = 0
#get reading at every 1 second
def get_analog_1(thread_name):
global read_ok_1, current_time_1,analog_1
while True:
if((time.time() - current_time_1) > 1):
if(0 == read_ok_1):
current_time_1 = time.time();
read_ok_1 = 1;
analog_1 = randint(0,100)
#get reading on update
def read_update_analog_1():
global read_ok_1,analog_1
data1 = 0
val1 = 0
if(1 == read_ok_1):
read_ok_1 = 0;
data1 = 1;
val1 = analog_1
return data1,val1
#get reading at every 10 second
def get_analog_2(thread_name):
global read_ok_2, current_time_2,analog_2
while True:
if((time.time() - current_time_2) > 10):
if(0 == read_ok_2):
current_time_2 = time.time();
read_ok_2 = 1;
analog_2 = randint(0,100)
#get reading on update
def read_update_analog_2():
global read_ok_2,analog_2
data2 = 0
val2 = 0
if(1 == read_ok_2):
read_ok_2 = 0;
data2 = 1;
val2 = analog_2
return data2,val2
[/code]
screen.py
[code]
from Tkinter import *
import analog
import thread
import time
class App():
def __init__(self, master):
self.master = master
frame = Frame(master)
frame.pack()
label = Label(frame , text = "Analog" , font = ("Helvetica",32))
label.grid(row = 0)
self.reading_label = Label(frame, text = '0.0' , font =
("Helvetica",70))
self.reading_label.grid(row = 1)
self.update_reading()
def update_reading(self):
data1,val1 = analog.read_update_analog_1()
if(1 == data1):
reading_str = "{:.1f}".format(val1)
self.reading_label.configure(text = reading_str)
self.master.after(100 , self.update_reading)
def display(threadName):
root = Tk()
root.wm_title("Ammeter")
app = App(root)
root.geometry("480x320")
root.mainloop()
[/code]
screen2.py
[code]from Tkinter import *
import analog
import thread
import time
class App():
def __init__(self, master):
self.master = master
frame = Frame(master)
frame.pack()
label = Label(frame , text = "Analog" , font = ("Helvetica",32))
label.grid(row = 0)
self.reading_label = Label(frame, text = '0.0' , font =
("Helvetica",70))
self.reading_label.grid(row = 1)
self.update_reading()
def update_reading(self):
data1,val1 = analog.read_update_analog_2()
if(1 == data1):
reading_str = "{:.1f}".format(val1)
self.reading_label.configure(text = reading_str)
self.master.after(1000 , self.update_reading)
def display(threadName):
root = Tk()
root.wm_title("Voltmeter")
app = App(root)
root.geometry("480x320")
root.mainloop()
[/code]
--
https://mail.python.org/mailman/listinfo/python-list