I wish to make a motor control app. I have 3 windows, which are as follows (unfortunately I cannot upload pictures yet, can I upload them some other way ):
So, what I want to do is when a user clicks then "GO" button the motor rotates and when the user clicks the "STOP" button the motor stops. After the "GO" button has been pressed I would also like to go to a different window such as the window in the top and bottom picture.
However, the GUI freezes after clicking the "GO" button. Is there a way that doesn't freeze the GUI and allow me to get the desired functionality?
#! usr/bin/env python
import threading
import serial
import time
import tkinter as tk
System_State_is_Run=False
class DH_Corp_SCADA2(tk.Tk):
def __init__(self, *args,**kwargs):
tk.Tk.__init__(self, *args, *kwargs)
container=tk.Frame(self)
container.pack(side="top",fill="both",expand=True)
container.grid_rowconfigure(0,weight=10)
container.grid_columnconfigure(0,weight=10)
self.frames={}
for F in (HomePage, ControlPage, Speed1Page):
frame=F(container,self)
self.frames[F]=frame
frame.grid(row=0, column=0, sticky="nsew")
self.show_frame(HomePage)
def show_frame(self,cont):
frame=self.frames[cont]
frame.tkraise()
class HomePage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
someLabel=tk.Label(self,text='Welcome to the AGC SCADA....')
AGC_Control=tk.Button(self,text='AGC Control & Parameters',
command=lambda:controller.show_frame(ControlPage))
AGC_Speed=tk.Button(self,text='Monitor Speed',command=lambda:
controller.show_frame(Speed1Page))
Exit_Button=tk.Button(self, text='Exit',command=close_window)
someLabel.grid(row=0, sticky="W")
AGC_Control.grid(row=2, column=1, sticky="EW",padx=2,pady=2)
AGC_Speed.grid(row=3,column=1, sticky="EW", padx=2,pady=2)
Exit_Button.grid(row=4,column=1, sticky="EW",padx=2,pady=2)
class ControlPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
someLabel1=tk.Label(self,text='From this page you may change p.')
Go_Home1=tk.Button(self,text='Home Page',command=lambda:controller.show_frame(HomePage))
AGC_Speed=tk.Button(self,text='Monitor Speed',command=lambda:controller.show_frame(Speed1Page))
Exit_Button=tk.Button(self, text='Exit',command=close_window)
Go_Button=tk.Button(self,text='GO',command=System_State_is_RunXY1)
Stop_Button=tk.Button(self,text='STOP', command=System_State_is_RunXY2)
someLabel1.grid(row=0, sticky="W")
Go_Button.grid(row=2,column=1, sticky="EW",padx=2,pady=2)
Stop_Button.grid(row=3,column=1, sticky="EW",padx=2,pady=2)
Go_Home1.grid(row=4, column=1, sticky="EW",padx=2,pady=2)
AGC_Speed.grid(row=5,column=1, sticky="EW", padx=2,pady=2)
Exit_Button.grid(row=6,column=1, sticky="EW",padx=2,pady=2)
class Speed1Page(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
someLabel2=tk.Label(self,text='From this page you can monitor speed parameter in channel 1 or left motor.')
Go_Home2=tk.Button(self,text='Home Page',command=lambda
:controller.show_frame(HomePage))
AGC_Control=tk.Button(self,text='AGC Control & Parameters',command=
lambda:controller.show_frame(ControlPage))
Exit_Button=tk.Button(self, text='Exit',command=close_window)
someLabel2.grid(row=0, sticky="W")
Go_Home2.grid(row=2,column=1, sticky="EW", padx=2,pady=2)
AGC_Control.grid(row=3, column=1, sticky="EW",padx=2,pady=2)
Exit_Button.grid(row=4,column=1, sticky="EW",padx=2,pady=2)
def System_State_is_RunXY1():
global System_State_is_Run
System_State_is_Run=True
print(System_State_is_Run)#check if System_State_is_Run changes
running_car()
def System_State_is_RunXY2():
global System_State_is_Run
System_State_is_Run=False
print(System_State_is_Run)#check if System_State_is_Run changes
running_car()
def running_car():
print(System_State_is_Run)#check if System_State_is_Run changes
if System_State_is_Run==True:
motor_run_callback()
running_car()
else:
motor_off_callback()
return
def motor_run_callback():# set motor speed to 25rpm
ser = serial.Serial('COM6',115200,timeout=0)
time.sleep(0.025)
ser.write(bytes('!G 1 250\r','utf-8'))
ser.close()
return
def motor_off_callback(): #set motor speed to 0
ser = serial.Serial('COM6',115200,timeout=0)
time.sleep(0.025)
ser.write(bytes('!G 1 0\r','utf-8'))
ser.close()
def close_window():
app.destroy()
app=DH_Corp_SCADA2()
app.mainloop()