[英]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
使用的整个主程序from
于import
需要的时候每一个项目?
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_gui
或link_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.