Python:如何为GUI类创建单独的模块
此代码运行良好。MyApp是完成所有工作的类,MyGUI是显示和请求MyApp数据的用户界面Python:如何为GUI类创建单独的模块,python,user-interface,tkinter,python-module,Python,User Interface,Tkinter,Python Module,此代码运行良好。MyApp是完成所有工作的类,MyGUI是显示和请求MyApp数据的用户界面 class MyGUI(): # displays results from MyApp and sends request to MyApp (e.g. fetch prices new prices) def __init__(self): print("GUI running") def user_request_price(self,ticker):
class MyGUI(): # displays results from MyApp and sends request to MyApp (e.g. fetch prices new prices)
def __init__(self):
print("GUI running")
def user_request_price(self,ticker):
self.req_price(ticker)
# methods I request from MyApp
def req_price(self,ticker):
app.get_price(ticker)
# methods I receive from MyApp
def print_price(self,val,price):
print (val,":",price)
class MyApp(): # does a lot of stuff, e.g. fetch prices from a server
def __init__(self):
self.id = 0
self.gui = MyGUI() # start gui
# methods called by GUI
def get_price(self, ticker):
if ticker == "MSFT": price = 20.23
self.output_price(ticker,price)
# methods sent to GUI
def output_price(self,ticker,price):
self.gui.print_price(ticker,price)
if __name__ == "__main__":
app = MyApp()
app.gui.user_request_price("MSFT")
现在,我想将GUI放入一个单独的模块中,以便创建一个模块文件GUI.py并将其导入MyApp文件:
from gui import *
就这样。我挣扎的地方:gui.py看起来怎么样?MyGUI()如何访问MyApp方法?这样做明智吗?关于构造的任何其他建议?gui.py文件只需
class MyGUI(): # displays results from MyApp and sends request to MyApp (e.g. fetch prices new prices)
def __init__(self):
print("GUI running")
def user_request_price(self,ticker):
self.req_price(ticker)
# methods I request from MyApp
def req_price(self,ticker):
app.get_price(ticker)
# methods I receive from MyApp
def print_price(self,val,price):
print (val,":",price)
将导入添加到myapp.py的顶部,一切都会正常工作
如果有意义的话,我会尝试将代码分成不同的文件。它使阅读更加清晰。在您的
gui.py
from myapp import MyApp
class MyGUI(): # displays results from MyApp and sends request to MyApp (e.g. fetch prices new prices)
app = MyApp()
def __init__(self):
print("GUI running")
def user_request_price(self,ticker):
self.req_price(ticker)
# methods I request from MyApp
def req_price(self,ticker):
app.get_price(ticker)
# methods I receive from MyApp
def print_price(self,val,price):
print (val,":",price)
并在您的myapp.py
(注意第一行),确保两个文件位于同一目录中,否则必须相对更改导入
from gui import MyGUI
class MyApp(): # does a lot of stuff, e.g. fetch prices from a server
def __init__(self):
self.id = 0
self.gui = MyGUI() # start gui
# methods called by GUI
def get_price(self, ticker):
if ticker == "MSFT": price = 20.23
self.output_price(ticker,price)
# methods sent to GUI
def output_price(self,ticker,price):
self.gui.print_price(ticker,price)
if __name__ == "__main__":
app = MyApp()
app.gui.user_request_price("MSFT")
最后,我做到了这一点——这似乎是在应用程序和gui之间实现清晰分离和通信的最佳方法 图形用户界面: 应用程序:
是的,把他们俩分开没关系。而且,通过import all“gui.py”第11行的
导入后端方法很容易。name错误:名称“app”未定义我已经更新了代码,但是您的示例似乎在两个类之间有很多交错。但是,您必须始终尝试实现类之间的单向依赖。因此,当您编写代码时,请尝试在一个类中编写所有后端方法,并将整个类导入到前端文件中。不要将前端方法带入后端类。谢谢,但仍然不起作用。>ImportError:无法导入名称“MyGUI”,因此至少无法导入pyhton 3.6。禁止相互参照。您的评论很有道理,但如果您想严格地将GUI与核心分离,这就是本质。GUI向core发送消息并从core接收消息。非常开放的通用方法来解决这个问题。
import queue
def __init__(self):
threading.Thread.__init__(self)
self.requests = queue.Queue() # request queue for App
self.start()
def queue_request(self,reqId,val):
self.requests.put([reqId,val])
import threading
import queue
def checkGUIQueue(self):
threading.Timer(1.0, self.checkGUIQueue).start() # check every 1 second
while not self.gui.requests.empty():
(id,value) = self.gui.requests.get()
... process request ...