im fairly new to python Tkinter. I've done some googling but couldnt find a good answer.
i have a Tkinter GUI that wold do some function if there is some command detected through the serial port. I have a script that acts like a serial port listener called listener.py
. I want this listener.py
to talk to my tkinter program and execute a function. For example, from below code, i want my listener.py
to execute self.Start_Test_Button
. how could this be done please?
below is my listener.py
code :
def ReadFromPort(port) :
if os.path.exists(port) :
ser = serial.Serial(
port=port,\
baudrate=9600,\
parity=serial.PARITY_NONE,\
stopbits=serial.STOPBITS_ONE,\
bytesize=serial.EIGHTBITS,\
timeout=0)
#this will store the list
seq = []
count = 1
while True:
time.sleep(1)
for c in ser.read():
seq.append(c)
joined_seq = ''.join(str(v) for v in seq) #Make a string from array
if c == '\n':
received = joined_seq.strip("\n")
received = received.strip('\r')
received = received.strip()
#print received
if "Test" in received :
#print "got it"
#print received
ser.close()
return "GO"
sys.exit()
else :
return "Port Not Found"
-- and below is my Tkinter code :--
#!/usr/bin/python
import "whatever needed"
class myThread (threading.Thread):
def __init__(self, name, ser):
threading.Thread.__init__(self)
self.name = name
self.ser = ser
self.buf = ""
class PT_Inline(Tkinter.Tk):
def __init__(self,parent):
Tkinter.Tk.__init__(self,parent)
self.parent = parent
self.initialize()
def initialize(self):
self.geometry("600x400")
self.resizable(1,1)
self.title("Some Title")
self.configure(background = 'white')
#self.attributes('-alpha', 0.5)
Function_Button1 = Tkinter.Button(self, text = "Start Test", fg = "black", bg = 'whitesmoke', width = 15, height = 2, font = "ArialBlack 7 bold roman", command = self.Start_Test_Button)
Function_Button1.place(relx = .6, rely = .8, anchor = "c")
Function_Button1.config(highlightbackground = "black")
Function_Button1.config(highlightthickness = 1)
#Function_Button1.config(state=DISABLED)
def Start_Test_Button_funcs(self) :
self.labelVariable_status.set("Waiting For Command")
port = "/dev/ttyACM1"
Read = ReadFromPort(port)
print Read
if Read == "PortNotFound" :
self.serial_number = "N/A"
fail_message = Read
self.Raise_ERROR(self.serial_number, fail_message)
elif Read == "GO" :
"Do ALL Functions Here"
def Start_Test_Button(self) :
t = threading.Thread(target = self.Start_Test_Button_funcs)
t.start()
if __name__ == "__main__":
app = PT_Inline(None)
app.title('Title')
subprocess.Popen(app.mainloop())