I don't know anything about pyserial, but it sounds like ser.read(1)
is blocking until it returns one byte. If that is a blocking call, that will freeze your GUI. You will have to either configure the serial port to allow non-blocking calls, or do the read in a separate thread.
Tkinter Freezing when calling Pyserial Function from Button
题
I am creating a graphical interface for my serial communications program. The layout for the gui is set up how I want and I have begun to try and add some of the serial code. However, I have run across a problem. When I click the "Test Connection" button or b1, the program freezes up and stops responding. Not the following code left out things I thought unnecessary. I have narrowed the problem down to read = ser.read(1). For some reason ser.read() is killing it. Thanks for any help in advance!
class COMS_app(Tkinter.Tk):
def __init__(self,parent):
Tkinter.Tk.__init__(self,parent)
self.parent = parent
self.platform_check()
self.initialize()
def initialize(self):
self.grid()
self.scrollbar = Tkinter.Scrollbar(self, orient = 'vertical', bd = 5)
self.scrollbar.grid(column = 2, row = 0, rowspan = 2, sticky = 'NSE')
self.com = Tkinter.Text(self, fg = "blue", height = 20, borderwidth = 5, padx = 5, pady = 5, relief = 'ridge')
self.com.grid(column = 0, row =0, columnspan = 2)
self.com.insert('1.0', 'COMS Terminal:')
self.scrollbar.config(command = self.com.yview)
self.inf = Tkinter.Text(self, fg = "blue", height = 8, borderwidth = 5, padx = 5, pady = 5, relief = 'ridge')
self.inf.grid(column = 0, row =7, columnspan = 2)
self.tb2Variable = Tkinter.StringVar()
self.tb2 = Tkinter.Entry(self,textvariable = self.tb2Variable)
self.tb2.grid(column=1,row=2, sticky='W')
self.tb2.bind("<Return>", self.OnPressEnter)
self.tb2Variable.set(u"Enter File Name")
b1 = Tkinter.Button(self, text = u"Test Connection", borderwidth = 5, relief = 'ridge', command = self.OnButtonClick1)
b1.grid(column = 0, row = 1, sticky = 'W')
b2 = Tkinter.Button(self, text = u"Receive File", borderwidth = 5, relief = 'ridge', command = self.OnButtonClick2)
b2.grid(column = 0, row = 2, sticky = 'W')
b3 = Tkinter.Button(self, text = u"Send AT C/D", borderwidth = 5, relief = 'ridge', command = self.OnButtonClick3)
b3.grid(column = 0, row =3, sticky = 'W')
b4 = Tkinter.Button(self, text = u"Send Text", borderwidth = 5, relief = 'ridge', command = self.OnButtonClick4)
b4.grid(column = 0, row = 4, sticky = 'W')
b5 = Tkinter.Button(self, text = u"End Program", borderwidth = 5, relief = 'ridge', command = self.OnButtonClick5)
b5.grid(column = 0, row = 5, sticky = 'W')
b6 = Tkinter.Button(self, text = u"Get Help", borderwidth = 5, relief = 'ridge', command = self.OnButtonClick6)
b6.grid(column = 0, row = 6, sticky = 'W')
self.resizable(False, False)
def OnButtonClick1(self):
self.test_con()
def OnButtonClick2(self):
pass
def OnButtonClick3(self):
pass
def OnButtonClick4(self):
pass
def OnButtonClick5(self):
pass
def OnButtonClick6(self):
pass
def OnPressEnter(self,event):
pass
def test_con(self):
read = ser.read(1)
tran = 'OUTWAY'
rec = 'INWAY'
ser.write(tran)
self.inf.insert(Tkinter.INSERT, '\n<<' + tran)
while not read:
pass
if read == rec:
self.inf.insert(Tkinter.INSERT, '\n<<' +rec)
elif read == FALSE:
self.inf.insert(Tkinter.INSERT, '\n- - NO RESPONSE FROM SERVER')
else:
self.inf.insert(Tkinter.INSERT, '\n- - INVALID RESPONSE FROM SERVER: SERVER:<<' + read)
if __name__ == "__main__":
app = COMS_app(None)
app.title('INS Communications Beta 2.0')
app.mainloop()
解决方案
不隶属于 StackOverflow