0

Just to make a clear distinction between my questions and a lot of other questions on here:

I have already written the 'main' program (which has classes, functions, and variables alike) and a good chunk of the GUI.

So this isn't a question on how to write in tkinter or python, but more so how can I combine them?

Should I run the program from the GUI? And import the various variables, functions, and classes? And if so, should I import the whole main program of use from to import each item when needed?

Should I create a third program that imports the main and the GUI?

I just can't seem to find any clear answer, or at least I can't seem to find out how to even phrase the question because all search results point to how to write GUI, which I already get the gist of.

martineau
  • 119,623
  • 25
  • 170
  • 301
Maz
  • 35
  • 8
  • 1
    Will you ever wish to run the application without the GUI? or is the application always going to be run with the GUI? –  Feb 24 '20 at 12:14
  • No I plan to run my programs with a GUI – Maz Feb 24 '20 at 12:36
  • Does this answer your question? https://stackoverflow.com/questions/59919233/import-another-module-in-main-page/59934558#59934558 – DaniyalAhmadSE Feb 24 '20 at 14:06
  • No sorry, I'm looking for how to combine modules with other GUI modules – Maz Feb 24 '20 at 14:56

1 Answers1

1

Here is an example of structure I did for one of my projects containing a Server (your actual main code), a GUI, and a third program I called "App" that just runs the 2. I created functions like link_with_gui or link_with_server so you can access to your GUI's variables from the Server and vice-versa.

To run this program, you just have to call python app.py. I added sections if __name__ == '__main__' in the Server and GUI so you can run them independantly (for testing purposes).

EDIT : I updated my code with threads. In the Server, you have an infinite loop that increments the variable self.count every second, and in the GUI if you click on the button, it will print this count.

App :

# app.py
from server import Server
from gui import GUI

class App:
    def __init__(self):
        self.gui = GUI()
        self.server = Server()
        self.link_parts()

    def link_parts(self):
        self.server.link_with_gui(self.gui)
        self.gui.link_with_server(self.server)

def main():
    app = App()
    app.gui.mainloop()

if __name__ == '__main__':
    main()

Server :

# server.py
import threading
import time

class Server:
    def __init__(self):
        self.count = 0
        thread = threading.Thread(target=self.counter)
        thread.daemon = True
        thread.start()

    def link_with_gui(self, gui):
        self.gui = gui

    def display(self):
        self.gui.chat_text.delete("1.0", "end")
        self.gui.chat_text.insert("insert", "This is Server count : {}".format(self.count))

    def counter(self):
        while True:
            self.count += 1
            time.sleep(1)
            print("self.count", self.count)

if __name__ == '__main__':
    server = Server()
    time.sleep(4)

GUI :

# gui.py
import tkinter as tk
class GUI(tk.Tk): # Graphic User Interface
    def __init__(self):
        super().__init__()
        self.btn = tk.Button(master=self, text="Click Me")
        self.btn.pack()

        self.chat_text = tk.Text(self, width=20, height=3)
        self.chat_text.pack()

    def link_with_server(self, server):
        self.server = server
        self.btn.configure(command=self.server.display)

if __name__ == '__main__':
    gui = GUI()
    gui.mainloop()
Phoenixo
  • 2,071
  • 1
  • 6
  • 13
  • So in your example, you're running the whole program from the app.py module? – Maz Feb 24 '20 at 12:38
  • Yes, all is command from app module. If you need to have multiple loops (the gui main loop, and a game loop for example), then use threads (module threeading) – Phoenixo Feb 24 '20 at 12:40
  • Okay I'll have to try and incorporate your example into my program. I'll also take a look at module threading right now – Maz Feb 24 '20 at 12:42
  • Oh okay, even just at a first glance, it looks like module threading is essentially a means of running more than one module at once? – Maz Feb 24 '20 at 12:44
  • Since the GUI is an infinite loop, if you want to have several functions running in parallel, you will need threads. Imagine you press a button that starts a long process. Without thread, you will have to wait for this process to finish before you can press another button, which is awful for user experience – Phoenixo Feb 24 '20 at 12:56
  • @Maz : I updated my answer with threads. In the Server, you have an infinite loop that increments the variable `self.count` every second, and in the GUI if you click on the button, it will print this `count`. Try and see :) – Phoenixo Feb 24 '20 at 13:09
  • Wow I really appreciate the help. I'll have to do that when I get home. I'm using pydroid right now and it's powerful mostly, but still pretty limited. – Maz Feb 24 '20 at 13:39
  • You're welcome. I was in your situation few months ago, and I didn't find on Stack Overflow any "good structure" for an app with GUI, so I had to find myself, and here's my "cleanest" result. Don't hesitate if you have more questions. Good luck ! – Phoenixo Feb 24 '20 at 13:44
  • Hey @phoenixo, one more similar question if you don't mind. Is there anyway I can message you? I'm pretty new to this site – Maz Feb 24 '20 at 16:46
  • No, I don't think it is possible to send private messages on Stack Overflow. I guess the purpose of this site is to have information visible by all – Phoenixo Feb 24 '20 at 21:48