Python tkinter Gui stops logic of Main class

Viewed 35

I have this kind of code where I wanted to add gui to my app and when I initiaze Main class the code runs till it hits Gui, then It open the gui window but the code won't go freely to the infinte loop unless I close the gui window.

My question is how to implement gui this main class so it to codes with the logic runs infinitely and the gui just display the data as it should

import gui as G
    
class Main:
    timeDelay = open("delay.txt", "r")
    global delay
    delay = int(timeDelay.read())
    def __init__(self):
        R.readTermMeter()
        X.checkTermMetersInDb(R.termListFromXml)
        P.getIdOfTeplomer(R.termListFromXml)



        G.Gui.__init__(self)
        gui = G.Gui()

        i = 5
        while i > 1:
            for item in R.termListFromXml:
                P.sendDataToDb(item)
            time.sleep(delay)



if __name__ == '__main__':
    Main()


/////////

class Gui:
    def __init__(self):
        window = Tk()

        window.geometry("1053x1000")
        window.configure(bg = "#FFFFFF")


        canvas = Canvas(
            window,
            bg = "#FFFFFF",
            height = 1000,
            width = 1053,
            bd = 0,
            highlightthickness = 0,
            relief = "ridge"
        )

        canvas.place(x = 0, y = 0)
        canvas.create_rectangle(
            580.0,
            0.0,
            1053.0,
            1000.0,
            fill="#1AACFF",
            outline="")

        canvas.create_text(
            98.0,
            42.0,
            anchor="nw",
            text="last data: ",
            fill="#000000",
            font=("RobotoRoman Bold", 30 * -1)
        )

        canvas.create_rectangle(
            619.0,
            120.0,
            1015.0,
            815.0,
            fill="#E2E2E2",
            outline="")

        canvas.create_text(
            633.0,
            43.0,
            anchor="nw",
            text="log:",
            fill="#000000",
            font=("RobotoRoman Bold", 30 * -1)
        )

        button_image_1 = PhotoImage(
            file=relative_to_assets("button_1.png"))
        button_1 = Button(
            image=button_image_1,
            borderwidth=0,
            highlightthickness=0,
            command=lambda:[print("button_1 clicked"),btnUpdateData()],
            relief="flat"
        )
        button_1.place(
            x=835.0,
            y=34.0,
            width=180.0,
            height=55.0
        )

        canvas.create_rectangle(
            35.0,
            123.0,
            527.0,
            183.0,
            fill="#D9D9D9",
            outline="")

        delkaListu = len(T.termListFromXml)
        i = int(delkaListu)




        for x, i in enumerate(T.termListFromXml):
                canvas.create_rectangle(
                    35.0,
                    123.0 + 70 * x,
                    213.0,
                    183.0 + 70 * x,
                    fill="#EB8888",
                    outline="")

                nameText = tkinter.Label(
                    text=i.name,
                    justify="center",
                    height="1",
                    font=("Georgia", int(16.0)),
                    bg="#EB8888"
                )
                nameText.place(
                    x=45,
                    y=138 + 70 * x,
                )



                canvas.create_rectangle(
                    213.0,
                    123.0 + 70 * x,
                    400.0,
                    183.0 + 70 * x,
                    fill="#7EB7EC",
                    outline="")


                ipText = tkinter.Label(
                    text=i.ip_add,
                    justify="center",
                    height="1",
                    font=("Georgia", int(16.0)),
                    bg="#7EB7EC"
                )
                ipText.place(
                    x=220,
                    y=138 + 70 * x,
                )

                if isinstance(i, O.Oboji):
                    canvas.create_rectangle(
                        400.0,
                        123.0 + 70 * x,
                        527.0,
                        183.0 + 70 * x,
                        fill="#FF4B4B",
                        outline="")

                    valueText = tkinter.Label(
                        text=i.teplotaValue,
                        justify="center",
                        height="1",
                        font=("Georgia", int(16.0)),
                        bg="#FF4B4B"
                    )
                    valueText.place(
                        x=410,
                        y=138 + 70 * x,
                    )
                    canvas.create_rectangle(
                        400.0,
                        123.0 + 70 * x,
                        527.0,
                        183.0 + 70 * x,
                        fill="#FF4B4B",
                        outline="")

                    valueText = tkinter.Label(
                        text=i.vlhkostValue,
                        justify="center",
                        height="1",
                        font=("Georgia", int(16.0)),
                        bg="#FF4B4B"
                    )
                    valueText.place(
                        x=410,
                        y=138 + 70 * x,
                    )

                else:
                    canvas.create_rectangle(
                        400.0,
                        123.0 + 70 * x,
                        527.0,
                        183.0 + 70 * x,
                        fill="#FF4B4B",
                        outline="")

                    valueText = tkinter.Label(
                        text="xx",
                        justify="center",
                        height="1",
                        font=("Georgia", int(16.0)),
                        bg="#FF4B4B"
                    )
                    valueText.place(
                        x=410,
                        y=138 + 70 * x,
                    )

        # for x in (T.termListFromXml):
            # canvas.itemconfigure(nameText,text=x.name)
            # nameText.configure(textvariable=x.name)

        def btnUpdateData():
            for x, i in enumerate(T.termListFromXml):
                if isinstance(i, O.Oboji):
                    print(i.teplotaValue)

                    print(i.vlhkostValue)



                else:
                    print(i.value)

        window.resizable(False, False)
        window.mainloop()


if __name__ == "__main__":
    Gui()

1 Answers

you need to separate the init stuff from the infinite loop you are trying to execute.

In the init method - only do stuff that needs to be done once.

In a run method - use a while True loop and put your logic in this loop.

Then, in the if name == 'main' part, first, initialize your object, and on the next line, trigger its run method.

    if __name__ == '__main__':
        runner = Main()
        Main.run()

Hope this helps!

Related