簡體   English   中英

Python:如何為 GUI 類創建單獨的模塊

[英]Python: How to create a separate module for the GUI class

這段代碼工作正常。 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 方法? 進行這種分離是否明智? 關於結構化的任何其他建議?

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 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 ...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM