简体   繁体   English

如何将我的主程序与 GUI“合并”?

[英]How can I 'merge' my main program with a GUI?

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.已经编写了“主”程序(它有类、函数和变量)和大量的 GUI。

So this isn't a question on how to write in tkinter or python, but more so how can I combine them?所以这不是关于如何用 tkinter 或 python 编写的问题,而是我如何它们结合起来?

Should I run the program from the GUI?我应该从 GUI 运行程序吗? And import the various variables, functions, and classes?import各种变量、函数和类? And if so, should I import the whole main program of use from to import each item when needed?如果是的话,我应该import使用的整个主程序fromimport需要的时候每一个项目?

Should I create a third program that imports the main and the GUI?我应该创建第三个程序来导入主程序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.我似乎无法找到任何明确的答案,或者至少我似乎无法找出如何表达这个问题,因为所有搜索结果都指向如何编写 GUI,我已经掌握了它的要点。

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.这是我为我的一个项目所做的结构示例,其中包含一个服务器(您的实际主代码)、一个 GUI 和我称为“App”的第三个程序, link_with_gui运行 2。我创建了像link_with_guilink_with_server这样的函数,所以你可以从服务器访问您的 GUI 变量,反之亦然。

To run this program, you just have to call python app.py .要运行这个程序,你只需要调用python app.py I added sections if __name__ == '__main__' in the Server and GUI so you can run them independantly (for testing purposes).我在服务器和 GUI 中添加了if __name__ == '__main__' ,以便您可以独立运行它们(用于测试目的)。

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.在服务器中,您有一个无限循环,每秒递增变量 self.count,在 GUI 中,如果您单击按钮,它将打印此计数。

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()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将我的 python gui 程序存储为桌面应用程序? - how can i store my python gui program as desktop app? 我怎样才能让我的主程序首先出现,我怎样才能在我的主程序中调用我的函数? - How can I make my main program appear first and how can I call my functions in my main program? 将我的 Tkinter GUI 连接到我的主程序 - Connect my Tkinter GUI to my main program 程序的主循环可以移出GUI吗? - Can the main loop of a program be moved out of GUI? 如何将我的 python 逻辑应用到 tkinter GUI 中? 这是一个简单的 GET 请求程序 - How can i apply my python logic into the tkinter GUI? It's a simple GET request program 如何使我的GUI程序(tkinter)在没有安装python的计算机上工作,从而不会显示控制台窗口? - How can I make my GUI program(tkinter) work on computers with no python installed such that no console window appears? 如何使用 Python 和 PyQt 对我的 GUI 程序进行单元测试? - How do I unit testing my GUI program with Python and PyQt? 如果主程序使用特定变量,如何在我的模块中计算? - How can I figure out in my module if the main program uses a specific variable? 如何在与主程序不同的线程中编写套接字服务器(使用gevent)? - How can I write a socket server in a different thread from my main program (using gevent)? 如何修复此网格板以不与程序中的主要细节重叠? - How can i fix this grid board to not to be overlapping the main details in my program?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM