I’m building a tkinter GUI to retrieve incoming data from a device (through request) and appending them into a csv file, while having a preview of these data.
I use thread
but struggle to understand it properly, especially how to pause and restart it.
When I click on the start button, my threads are working properly. Once I try to click the stop streaming button, my stop_threads
variable change to 0, however, the while loop in my thread do not see this change. Do you know why?
Thanks a lot!
JavaScript
x
145
145
1
import tkinter as tk
2
import threading
3
from tkinter import ttk
4
5
6
def init_csv():
7
pass
8
# create my csv file here
9
10
def clear_data():
11
tv1.delete(*tv1.get_children())
12
init_csv()
13
14
15
def stream_data():
16
global stop_threads
17
IP_Machine = IP_entry.get()
18
19
while True:
20
if stop_threads == 1:
21
print('Stream')
22
# retrieve incoming data from my device (through request)
23
# appending them into a csv file
24
25
if stop_threads == 0:
26
print('Pause')
27
# I want to pause this thread here
28
29
30
def start_stream():
31
t1_stream.start()
32
stream_label['text'] = 'Streaming...'
33
button_start.destroy()
34
35
# Create stop streaming button
36
button_stop = tk.Button(main_frame, text="Stop Streaming", bg=Blue, fg=Bg_color, font='BOLD 12',
37
command=stop_stream)
38
button_stop.place(relx=0.2, rely=0.14, relwidth=0.15, relheight=0.07)
39
40
41
def stop_stream():
42
stop_threads = 0
43
stream_label['text'] = ''
44
45
# Create ReStart streaming button
46
button_restart = tk.Button(main_frame, text="Start Streaming", bg=Blue, fg=Bg_color, font='BOLD 12',
47
command=restart)
48
button_restart.place(relx=0, rely=0.14, relwidth=0.15, relheight=0.07)
49
50
51
def restart():
52
stop_threads = 1
53
stream_label['text'] = 'Streaming...'
54
55
56
def curve():
57
# show live plot data with matplot Funcanimation
58
pass
59
60
61
def refresch_treeview():
62
tv1.delete(*tv1.get_children())
63
# define content of the treeview and refrech
64
tv1.after(500, refresch_treeview)
65
66
67
csv_path = 'C:/Users/b/Desktop/testfile.csv'
68
init_csv()
69
global stop_threads
70
stop_threads = 1
71
72
# Multitasking definition
73
t1_stream = threading.Thread(target=stream_data)
74
t2_refresch = threading.Thread(target=refresch_treeview)
75
76
# My Tkinter interface bellow
77
root = tk.Tk()
78
79
# Variables
80
Bg_color = '#F2F2F2'
81
Blue = '#045FB4'
82
c_width = 1400
83
c_height = 700
84
85
# Template
86
canvas = tk.Canvas(root, height=c_height, width=c_width, bg=Bg_color)
87
canvas.pack()
88
left_frame = tk.Frame(root, bg=Blue)
89
left_frame.place(anchor='center', relx=0.05, rely=0.5, relwidth=0.002, relheight=0.9)
90
right_frame = tk.Frame(root, bg=Blue)
91
right_frame.place(anchor='center', relx=0.95, rely=0.5, relwidth=0.002, relheight=0.9)
92
top_frame = tk.Frame(root, bg=Blue)
93
top_frame.place(anchor='center', relx=0.5, rely=0.05, relwidth=0.9, relheight=0.004)
94
bottom_frame = tk.Frame(root, bg=Blue)
95
bottom_frame.place(anchor='center', relx=0.5, rely=0.95, relwidth=0.9, relheight=0.004)
96
main_frame = tk.Frame(root, bg=Bg_color)
97
main_frame.place(anchor='center', relx=0.5, rely=0.5, relwidth=0.8, relheight=0.8)
98
# Title
99
title = tk.Label(root, text='Data analysis', height=2, width=20, font='bold 18')
100
title.place(anchor='center', relx=0.50, rely=0.05)
101
102
# ENTRIES Definition
103
# Machine IP
104
IP_frame = tk.Frame(main_frame, bg=Bg_color)
105
IP_frame.place(relx=0, rely=0.05, relwidth=0.35, relheight=0.05)
106
IP_label = tk.Label(IP_frame, text='2000Xc IP address : ', font='12')
107
IP_label.pack(side='left', fill='y')
108
IP_entry = tk.Entry(IP_frame, width=22, font='12')
109
IP_entry.pack(side='right', fill='y')
110
111
# Streaming runing label
112
stream_label = tk.Label(main_frame, font='10', text='')
113
stream_label.place(anchor='w', relx=0, rely=0.45, relwidth=0.1, relheight=0.06)
114
115
# BUTTONS Definition
116
# Start streaming button
117
button_start = tk.Button(main_frame, text="Start Streaming", bg=Blue, fg=Bg_color, font='BOLD 12', command=start_stream)
118
button_start.place(relx=0, rely=0.14, relwidth=0.15, relheight=0.07)
119
120
# Clear data
121
button_clear = tk.Button(main_frame, text="Clear data", bg=Blue, fg=Bg_color, font='BOLD 10', command=clear_data)
122
button_clear.place(relx=0.92, rely=0.9, relwidth=0.08, relheight=0.06)
123
# Show graph button
124
button_graph = tk.Button(main_frame, text="Show curves", bg=Blue, fg=Bg_color, font='BOLD 10', command=curve)
125
button_graph.place(relx=0.6, rely=0.35, relwidth=0.1, relheight=0.05)
126
127
# csv preview
128
preview_frame = tk.Frame(main_frame, bg=Bg_color)
129
preview_frame.place(relx=0, rely=0.5, relwidth=1, relheight=0.4)
130
tv1 = ttk.Treeview(preview_frame)
131
tv1.place(relheight=0.95, relwidth=1) # set the height and width of the widget to 100% of its container (frame1).
132
# scroll preview
133
treescrolly = tk.Scrollbar(preview_frame, orient="vertical",
134
command=tv1.yview) # command means update the yaxis view of the widget
135
treescrollx = tk.Scrollbar(preview_frame, orient="horizontal",
136
command=tv1.xview) # command means update the xaxis view of the widget
137
tv1.configure(xscrollcommand=treescrollx.set,
138
yscrollcommand=treescrolly.set) # assign the scrollbars to the Treeview Widget
139
treescrollx.pack(side="bottom", fill="x") # make the scrollbar fill the x axis of the Treeview widget
140
treescrolly.pack(side="right", fill="y") # make the scrollbar fill the y axis of the Treeview widget
141
142
t2_refresch.start()
143
144
root.mainloop()
145
Advertisement
Answer
You have to change only one thing. You need to define stop_threads as global inside the functions. Python then knows that you are referring to the global variable instead of creating a new variable inside the local function scope.
JavaScript
1
147
147
1
import tkinter as tk
2
import threading
3
from tkinter import ttk
4
5
6
def init_csv():
7
pass
8
# create my csv file here
9
10
def clear_data():
11
tv1.delete(*tv1.get_children())
12
init_csv()
13
14
15
def stream_data():
16
global stop_threads
17
IP_Machine = IP_entry.get()
18
19
while True:
20
if stop_threads == 1:
21
print('Stream')
22
# retrieve incoming data from my device (through request)
23
# appending them into a csv file
24
25
if stop_threads == 0:
26
print('Pause')
27
# I want to pause this thread here
28
29
30
def start_stream():
31
t1_stream.start()
32
stream_label['text'] = 'Streaming...'
33
button_start.destroy()
34
35
# Create stop streaming button
36
button_stop = tk.Button(main_frame, text="Stop Streaming", bg=Blue, fg=Bg_color, font='BOLD 12',
37
command=stop_stream)
38
button_stop.place(relx=0.2, rely=0.14, relwidth=0.15, relheight=0.07)
39
40
41
def stop_stream():
42
global stop_threads
43
stop_threads = 0
44
stream_label['text'] = ''
45
46
# Create ReStart streaming button
47
button_restart = tk.Button(main_frame, text="Start Streaming", bg=Blue, fg=Bg_color, font='BOLD 12',
48
command=restart)
49
button_restart.place(relx=0, rely=0.14, relwidth=0.15, relheight=0.07)
50
51
52
def restart():
53
global stop_threads
54
stop_threads = 1
55
stream_label['text'] = 'Streaming...'
56
57
58
def curve():
59
# show live plot data with matplot Funcanimation
60
pass
61
62
63
def refresch_treeview():
64
tv1.delete(*tv1.get_children())
65
# define content of the treeview and refrech
66
tv1.after(500, refresch_treeview)
67
68
69
csv_path = 'C:/Users/b/Desktop/testfile.csv'
70
init_csv()
71
72
stop_threads = 1
73
74
# Multitasking definition
75
t1_stream = threading.Thread(target=stream_data)
76
t2_refresch = threading.Thread(target=refresch_treeview)
77
78
# My Tkinter interface bellow
79
root = tk.Tk()
80
81
# Variables
82
Bg_color = '#F2F2F2'
83
Blue = '#045FB4'
84
c_width = 1400
85
c_height = 700
86
87
# Template
88
canvas = tk.Canvas(root, height=c_height, width=c_width, bg=Bg_color)
89
canvas.pack()
90
left_frame = tk.Frame(root, bg=Blue)
91
left_frame.place(anchor='center', relx=0.05, rely=0.5, relwidth=0.002, relheight=0.9)
92
right_frame = tk.Frame(root, bg=Blue)
93
right_frame.place(anchor='center', relx=0.95, rely=0.5, relwidth=0.002, relheight=0.9)
94
top_frame = tk.Frame(root, bg=Blue)
95
top_frame.place(anchor='center', relx=0.5, rely=0.05, relwidth=0.9, relheight=0.004)
96
bottom_frame = tk.Frame(root, bg=Blue)
97
bottom_frame.place(anchor='center', relx=0.5, rely=0.95, relwidth=0.9, relheight=0.004)
98
main_frame = tk.Frame(root, bg=Bg_color)
99
main_frame.place(anchor='center', relx=0.5, rely=0.5, relwidth=0.8, relheight=0.8)
100
# Title
101
title = tk.Label(root, text='Data analysis', height=2, width=20, font='bold 18')
102
title.place(anchor='center', relx=0.50, rely=0.05)
103
104
# ENTRIES Definition
105
# Machine IP
106
IP_frame = tk.Frame(main_frame, bg=Bg_color)
107
IP_frame.place(relx=0, rely=0.05, relwidth=0.35, relheight=0.05)
108
IP_label = tk.Label(IP_frame, text='2000Xc IP address : ', font='12')
109
IP_label.pack(side='left', fill='y')
110
IP_entry = tk.Entry(IP_frame, width=22, font='12')
111
IP_entry.pack(side='right', fill='y')
112
113
# Streaming runing label
114
stream_label = tk.Label(main_frame, font='10', text='')
115
stream_label.place(anchor='w', relx=0, rely=0.45, relwidth=0.1, relheight=0.06)
116
117
# BUTTONS Definition
118
# Start streaming button
119
button_start = tk.Button(main_frame, text="Start Streaming", bg=Blue, fg=Bg_color, font='BOLD 12', command=start_stream)
120
button_start.place(relx=0, rely=0.14, relwidth=0.15, relheight=0.07)
121
122
# Clear data
123
button_clear = tk.Button(main_frame, text="Clear data", bg=Blue, fg=Bg_color, font='BOLD 10', command=clear_data)
124
button_clear.place(relx=0.92, rely=0.9, relwidth=0.08, relheight=0.06)
125
# Show graph button
126
button_graph = tk.Button(main_frame, text="Show curves", bg=Blue, fg=Bg_color, font='BOLD 10', command=curve)
127
button_graph.place(relx=0.6, rely=0.35, relwidth=0.1, relheight=0.05)
128
129
# csv preview
130
preview_frame = tk.Frame(main_frame, bg=Bg_color)
131
preview_frame.place(relx=0, rely=0.5, relwidth=1, relheight=0.4)
132
tv1 = ttk.Treeview(preview_frame)
133
tv1.place(relheight=0.95, relwidth=1) # set the height and width of the widget to 100% of its container (frame1).
134
# scroll preview
135
treescrolly = tk.Scrollbar(preview_frame, orient="vertical",
136
command=tv1.yview) # command means update the yaxis view of the widget
137
treescrollx = tk.Scrollbar(preview_frame, orient="horizontal",
138
command=tv1.xview) # command means update the xaxis view of the widget
139
tv1.configure(xscrollcommand=treescrollx.set,
140
yscrollcommand=treescrolly.set) # assign the scrollbars to the Treeview Widget
141
treescrollx.pack(side="bottom", fill="x") # make the scrollbar fill the x axis of the Treeview widget
142
treescrolly.pack(side="right", fill="y") # make the scrollbar fill the y axis of the Treeview widget
143
144
t2_refresch.start()
145
146
root.mainloop()
147