Skip to content Skip to sidebar Skip to footer

Python Tkinter Making Serial Work With Buttons

I'm new to OOP,and I'm trying to make a GUI for myself. I want to make a simulator program. I'm reading the serial data with an Arduino. My questions are: Why does the button dis

Solution 1:

Comment: when I press the quit button my window freezes, and can't do anything

I have overloaded the Application.destroy() which is calld from root.destroy(). There we call root.destroy(), which results in a infinit loop. Change def destroy(... to def quit(...


Question: how can I stop the run() function with the stop button

Use a flag variable e.g. self.stopUpdate as follows:

def__init__(self,master,*args,**kwargs):
   ...
    self.stopUpdate = None
    self.MakeSerial()

defPackWidgets(self):
    ...
    self.quit_button=Button(..., command=self.quit, ...

defquit(self):
    self.Stop()
    # Wait until Update returnswhile self.stopUpdate:
        time.sleep(1)
    root.destroy()

defStart(self):
    self.stopUpdate = False
    self.Update()

defStop(self):
    self.stopUpdate = TruedefUpdate(self):
    if self.stopUpdate:
        # Indicates Update returns, for self.destroy(...
        self.stopUpdate = Falsereturntry:
        ...

Remove this alltogether

#def run(self):#    self.MakeSerial()#    self.Update()#    root.mainloop()
  • moved self.MakeSerial() to __init__(...
  • moved self.Update() to self.Start(...
  • move root.mainloop() to the bottom of the script.

Note: It is bad, to catch multiple Exceptions with onetry: ... except: block. Use one for .ser.readline(... and one for =data

Post a Comment for "Python Tkinter Making Serial Work With Buttons"