0

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())
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
BarathanR
  • 151
  • 1
  • 2
  • 8
  • Have you tried replacing `"whatever needed"` with `listener`? – TigerhawkT3 Nov 30 '15 at 08:32
  • hi there sir! could you please explai? after importing listener.py, how do i call the self.Start_Test_Button function? i want to call for this function from the listener.py. – BarathanR Nov 30 '15 at 08:47
  • `self.Start_Test_Button` is already part of the Tkinter app. I'd recommend sticking with what you had, importing your `listener` module into the Tkinter app, and accessing those functions with the proper namespace. Check the linked question and read the [tutorial on packages](https://docs.python.org/3.4/tutorial/modules.html#packages). – TigerhawkT3 Nov 30 '15 at 08:50
  • hi, but my intention here is to start a function in the Tkinter program from an external script. – BarathanR Nov 30 '15 at 08:57
  • could you also please show me where is the duplicate question? i cant seem to find it and sorry im new to stack overflow – BarathanR Nov 30 '15 at 08:59
  • Refresh the page. The link to the duplicate is between the question's title and its body text. – TigerhawkT3 Nov 30 '15 at 09:00
  • Sir, i cant really find the relation between my question and the link you sent. please help me out. from my code above, how do i call for function self.Start_Test_Button() from listener.py? – BarathanR Nov 30 '15 at 09:07

0 Answers0