Tkinter GUI and reading serial
Question: I am new to python and tkinter... I am using Tkinter to display a gauge and
receive the info through serial com. I have my GUI ready and now need to read
the serial values.

The problem I am facing is that I am not being able to read continuously the
serial COM. I came across `self.after`, but it still does not work. Basically
it does not display any value on the console. Any idea what might be wrong?

This is the main code. I have another file meter.py that has the gauge design

    
    
    import tkinter as tk
    import meter as m
    import serial
    
    class Mainframe(tk.Frame):
    
       def __init__(self,master,*args,**kwargs):
          super(Mainframe,self).__init__(master,*args,**kwargs)
    
          self.meter = m.Meter(self,height = 400,width = 400)
          self.meter.setrange(20,90)
          self.meter.pack()
    
          tk.Scale(self,width = 15 ,from_ = 20, to = 90
          ,orient = tk.HORIZONTAL
          ,command = self.setmeter).pack()
    
          tk.Button(self,text = 'Quit',width = 15,command = master.destroy).pack()
          tk.Button(self,text = 'Zoom',width = 15).pack()
    
       def setmeter(self,value):
          value = int(value)
          self.meter.set(value)   
    
    class App(tk.Tk):
    
        def __init__(self):
            super(App,self).__init__()
    
            self.title('Try Meter')
    
            Mainframe(self).pack()
    
        def serie():
            ser = serial.Serial('COM2', 2400, timeout=1)
            line = ser.readline()   # read a '\n' terminated line
            print (line)
            self.after(100,serie) 
    
    
    App().mainloop()
    


Answer: The reason why your code doesn't work as expected is that you never call the
`serie()` method of the `App` class. If you modify your code to add this call,
perhaps at the end of the `__init__()` method, you should see serial data
being printed to the console.

However, handling serial IO and the user interface in the same thread isn't a
good idea. If there isn't any data to read from the serial port, `readline()`
will block for up to 1 second (the timeout value you set when you initialised
the serial port). This will block the tk main loop, and your user interface
will be completely unresponsive. In my opinion, the best way to fix this would
be to use one of the asynchronous IO libraries. I like
[Twisted](http://twistedmatrix.com/trac/) as it has built-in support for tk
([example](http://twistedmatrix.com/documents/13.2.0/core/howto/choosing-
reactor.html#auto16)) and serial communication, but there are several other
options. If this feels like overkill, you could also move the serial polling
code to its own thread or process using the `threading` or `multiprocessing`
modules respectively.

# Update

To answer the second part of your question:

In `readSerial()` you create a new instance of `Mainframe()`, but I think what
you actually want to do is use the instance you created in the `__init__()`
method of the `App` class. You'll need to store this as an attribute, using
code like `self.main_frame = Mainframe(self)`, then in `readSerial()` you can
access it using `root.main_frame.setmeter(50)`.

