【发布时间】:2019-02-19 04:03:55
【问题描述】:
此代码运行良好。 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):
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 方法?进行这种分离是否明智?还有其他关于结构的建议吗?
【问题讨论】:
-
是的,可以将它们分开。通过
from <module_name> import all可以轻松导入您的后端方法
标签: python user-interface tkinter python-module