2015-06-25 23 views
0

我對wxpython和python本身很有新意,對於基本問題抱歉。使用wxpython綁定包

我試圖用更簡單的方式來組織我的代碼來管理它。我創建了這樣一個簡單的例子,恢復我的問題。基本上它只是一個帶有打印信息按鈕的窗口。我把它分成三個簡單的包:

ef_Main.py - 這是它將導入UI和應用程序本身的主包。

ef_Tool.py - 這是運行所有重要代碼的應用程序,現在它只是一個打印語句,但將包含所有應用程序代碼。

ef_UI.py - 一個非常基本的使用wxpython的界面。

它應該是如何工作的:

運行ef_Main.py將導入接口(ef_UI.py)和主代碼(ef_Tool.py)。當在界面上點擊某物時,ef_Main會準備好併發送給ef_Tool來執行。

我的問題是:

我不知道如何使用綁定功能,這三個包連接。我相信它應該在ef_Main中,但它將如何從接口獲取信息並將其發送到ef_Tool.py。

如果我想從ef_Tool獲得一些輸出並將其發送回界面。我應該怎麼做。

這是我的代碼。

#ef_Main.py 

import wx 
import ef_UI as eU 
import ef_Tool as eT 


''' Here is where I don't know how to make it works, 
if I should put a class here or not, and how to bind 
this function with the impTool and impUI''' 

#class MyMain(self): 
def uiControls(self): 
    self.Bind(wx.EVT_BUTTON, eU.OnClick(self), eT.MyTools(self)) 


def main(): 
    app = wx.App(False) 
    frame = eU.MyFrame() 
    frame.Show() 
    app.MainLoop() 

if __name__ == "__main__": 
    main() 

=======================

#ef_Tool.py 

import wx 

'''just a simple as possible function to be execute when it is called ''' 
class MyTools(): 
    def OnClick(self, event): 
     #Value = self.MyTextCtrl.GetValue() 
     print "it is working! " 

============= ==========

#ef_UI.py 

import wx 

''' very simple interface with only a button and a TextCtrl ''' 
class MyFrame(wx.Frame): 
    def __init__(self): 
     wx.Frame.__init__(self, None, title="Menu Test") 
     self.panel = wx.Panel(self) 

     self.MyButton = wx.Button(self.panel, -1, "Button_test", (0, 0)) 
     self.MyTextCtrl = wx.TextCtrl(self.panel, -1, value="just a test", pos=(100, 0)) 

在此先感謝!

Emerson

回答

0

下面是滿足您的要求的一個簡單示例。假設您的所有應用程序邏輯都在ef_Tool.py中完成,並且這些邏輯的輸入來自ef_UI,並且輸出也會發送到ef_UI.py

當在ef_UI中發生按鈕單擊事件時,您需要調用ef_Tool.py中的方法。您可以從MyFrame的方法調用此方法。但是你需要一個對象MyTools來做到這一點。

所以首先,在ef_Main.py你MyFrame類創建MyTools的OBJ,並通過這個對象來MyFrame

#ef_Main.py 

import wx 
import ef_UI as eU 
import ef_Tool as eT 


def main(): 
    efToolObj = eT.MyTools() # object of MyTools class 
    app = wx.App(False) 
    frame = eU.MyFrame(efToolObj) # Pass this to MyFrame so that it can make use of it 
    frame.Show() 
    app.MainLoop() 

if __name__ == "__main__": 
    main() 

商店這個MyTools的對象。然後用這個對象調用內部MyTools

#ef_UI.py 

import wx 

''' very simple interface with only a button and a TextCtrl ''' 
class MyFrame(wx.Frame): 
    def __init__(self, efToolObj): 
     wx.Frame.__init__(self, None, title="Menu Test") 
     self.panel = wx.Panel(self) 
     self.efToolObj = efToolObj # save the MyTools object to be used later 
     self.MyButton = wx.Button(self.panel, -1, "Button_test", (0, 0)) 
     self.MyButton.Bind(wx.EVT_BUTTON, self.onClickEvent) # Bind the click event to an event handling method 
     self.MyTextCtrl = wx.TextCtrl(self.panel, -1, value="just a test", pos=(100, 0)) 


    def onClickEvent(self,event): #this is called when a button is clicked 
     res = self.efToolObj.OnClickPrinting(self.MyTextCtrl.GetValue()) #Use the mytools object to call its method to apply logic,also get result values 
     self.MyTextCtrl.SetValue(res) #use the result values in your UI 

相應的方法你可以通過你要發送到應用程序邏輯中的參數和得到的結果作爲返回值的信息。

#ef_Tool.py 

class MyTools: 
    def OnClickPrinting(self,textvalue): 
     #Value = self.MyTextCtrl.GetValue() 
     print "it is working! ",textvalue 

     resultstr = "test successful" 
     return resultstr 

希望這對我有所幫助。

+0

謝謝你,它完美的作品! – Emerson