0

So i am trying to make a GUI using tkinter, that should read files from some other microcontollers. to do this i have made a Uart.py which i am pretty sure is working as it should. and then i try to pass its "work" over to another py file where i have my GUI in. but i cant get that part to work, where i am importing from the Uart.py

my Uart.py Code

import serial
import time

data = ""
dataTemp = "b'temperature temp read \r\n'"
dataPh = "b'ph probe read \r\n'"
value = ""
#dataWater = "b'ph dispenser write ' +str(int(waterAmount.get())1000).encode('ascii') + b' \r\n'"


class uart(object):
    uart = serial.Serial('/dev/serial0', baudrate=9600)

    def write(self, data):
        self.uart.write(data.encode())
        print("sent")
        time.sleep(3)

    def read(self, value):
        while True:
            self.value = self.uart.readline()
            time.sleep(2)
            print(self.value)
            value = self.value
            self.uart.flush()
            return value


if __name__ == '__main__':
     while True:

        def tempUart():
            myTemp = uart()
            myTemp.write(data=dataTemp)
            myTemp.read(value)

        def phUart():
            myPh = uart()
            myPh.write(data=dataPh)
            myPh.read(value)

 #      def waterUart():
 #           water = uart()
 #           water.write(data=dataWater)

And this is my Main Program GUI.py where i am importing it to and setting the value for ph by calling def phUart

import sys
from Uart import *
if sys.version_info[0] == 2:
    from Tkinter import *
else:
    from tkinter import *

PhSet = Uart.phUart()
tempSet = Uart.tempUart()

class gui:
    def __init__(self):
        self.tk = Tk()
        self.tk.attributes('-fullscreen', 'True')


        self.ph = Label(fg="black", background="#D6D7D6",text = "Ph: ",font=("Helvetica", 40, "bold"))
        self.ph.grid(column=0, row=0)
        self.temp = Label(fg="black", background="#D6D7D6",text = "Temp: ",font=("Helvetica", 40, "bold"))
        self.temp.grid(column=0, row=4)
        self.klor = Label(fg="black", background="#D6D7D6", text="Klortabletter tilbage: ", font=("Helvetica", 20,"bold"))
        self.klor.grid(column=8,row=8)
 #       self.waterAmount = Spinbox(from_=1000,to=100000,increment=1000,width=10, font=("Helvetica", 20,"bold"))
 #       self.waterAmount.grid(column=0, row=6)
 #       self.waterButtonSet = Button(text = 'Set water amount',command = set_waterAmount,font=("Helvetica",10,"bold"))
 #       self.waterButtonSet.grid(column=0,row=6)

        phSet = dataPh
        self.phSet = Label(width=5, fg="black", background="#D6D7D6", textvariable=dataPh, font=("Helvetica", 20,"bold"))
        self.phSet.grid(column=1, row=0)

        self.tempSet = Label(width=5, fg="black", background="#D6D7D6", textvariable=ph, font=("Helvetica", 20,"bold"))
        self.tempSet = grid(column=1, row=4)

        ## KLOR ##


if __name__ == '__main__':
    gui = gui()
    gui.tk.mainloop()

i have just started to program in python so i am not even sure if this is posible.

  • Read [While Loop Locks Application](https://stackoverflow.com/questions/28639228/python-while-loop-locks-application) – stovfl Dec 10 '19 at 15:27
  • Thanks but i dont think it helps much, cant seem to find the solution in there. my problem is that i want to get the def from Uart.py and use its varibel in GUI.py so i can use the Uart.py to read and write and just the other file as my GUI. for eks. updating phSet it should call the uart to get information and then set it in from that one? – Mads Vestergaard Dec 10 '19 at 15:31
  • How can you be sure that `uart.py` *works* when it doesn't do anything? You have a `while` loop, but nothing executes in it. – quamrana Dec 10 '19 at 15:35
  • @quamrana i have testet it on the raspberry before i tried to put it into the GUI. – Mads Vestergaard Dec 10 '19 at 15:42
  • @stovfl oh okay, so is not posible to do what i am trying to do? – Mads Vestergaard Dec 10 '19 at 15:42
  • 2
    @MadsVestergaard ***"is not posible to do what i am trying to do"***: It is possible, you have to change your module `uart.py` by moving the `__main__` part into your `class uart:` – stovfl Dec 10 '19 at 15:56
  • Okay I tried to do what I think you meant down in the new code, but now I can't get the variable? – Mads Vestergaard Dec 10 '19 at 18:36

1 Answers1

1

The problem I see is that Uart.py is running its main method. If you want to use Uart as a custom module, then you don't want Uart.py to run the __main__ loop. Instead, Uart.py should look something like:

import serial
import time

data = ""
dataTemp = "b'temperature temp read \r\n'"
dataPh = "b'ph probe read \r\n'"
value = ""
#dataWater = "b'ph dispenser write ' +str(int(waterAmount.get())1000).encode('ascii') + b' \r\n'"


class uart(object):
    uart = serial.Serial('/dev/serial0', baudrate=9600)

    def write(self, data):
        self.uart.write(data.encode())
        print("sent")
        time.sleep(3)

    def read(self, value):
        while True:
            self.value = self.uart.readline()
            time.sleep(2)
            print(self.value)
            value = self.value
            self.uart.flush()
            return value

Then, when using your uart() object in GUI.py, you would use it like so:

import sys
from Uart import *
if sys.version_info[0] == 2:
    from Tkinter import *
else:
    from tkinter import *

class gui:
    def __init__(self):
        self.phSet = uart()
        self.phSet.write(dataPh)
        self.tempSet = uart()
        self.tempSet.write(dataTemp)
        self.tk = Tk()
        self.tk.attributes('-fullscreen', 'True')


        self.ph = Label(fg="black", background="#D6D7D6",text = "Ph: ",font=("Helvetica", 40, "bold"))
        self.ph.grid(column=0, row=0)
        self.temp = Label(fg="black", background="#D6D7D6",text = "Temp: ",font=("Helvetica", 40, "bold"))
        self.temp.grid(column=0, row=4)
        self.klor = Label(fg="black", background="#D6D7D6", text="Klortabletter tilbage: ", font=("Helvetica", 20,"bold"))
        self.klor.grid(column=8,row=8)
 #       self.waterAmount = Spinbox(from_=1000,to=100000,increment=1000,width=10, font=("Helvetica", 20,"bold"))
 #       self.waterAmount.grid(column=0, row=6)
 #       self.waterButtonSet = Button(text = 'Set water amount',command = set_waterAmount,font=("Helvetica",10,"bold"))
 #       self.waterButtonSet.grid(column=0,row=6)

        self.phSet = Label(width=5, fg="black", background="#D6D7D6", textvariable=dataPh, font=("Helvetica", 20,"bold"))
        self.phSet.grid(column=1, row=0)

        self.tempSet = Label(width=5, fg="black", background="#D6D7D6", textvariable=ph, font=("Helvetica", 20,"bold"))
        self.tempSet = grid(column=1, row=4)

        ## KLOR ##

Notice a few changes made here:

  1. Since you use import * from Uart, you don't need to specify Uart.uart() when instantiating a new uart() object. This means we can just declare phSet in the gui object's constructor with self.phSet = uart(). The same holds true for tempSet.
  2. Removed the phSet = phData line from GUI.py. This is because, using the syntax from your version of Uart.py, it looks like you use the write() function to write the phData to the phSet. You can do this in the constructor for the gui() object as I have done in the above code.
  3. As an extension of the previous point, there is no reason to specify data=foo when passing arguments to the write() function. The data parameter does not need keyword arguments. Read more about default arguments and keyword arguments here.

I apologize if I misunderstand the purpose of your code, and have implemented incorrect 'fixes.' I think what you are getting at and what you need is just using custom modules in Python, and there are tons of ways you can implement this. I purposely left out re-writing a __main__ function for you, because you can do that either in GUI.py or you can write a new app.py or something of the likes to run your __main__ function, and that gives you more control over adding functions and classes to custom modules that you write elsewhere.

Hope this helps!

Z. Shaffer
  • 23
  • 5