使用wxpython绑定包

使用wxpython绑定包,python,data-binding,wxpython,packages,Python,Data Binding,Wxpython,Packages,我对wxpython和python本身有点陌生,对于这个基本的问题我很抱歉 我正试图以一种更简单的方式组织代码来管理它。我创建了一个简单的例子来解决我的问题。基本上,它只是一个带有按钮的窗口,用于打印消息。我把它分为三个简单的包 ef_Main.py-这是它将导入UI和应用程序本身的主要包 ef_Tool.py-应用程序将运行所有重要的代码,现在它只是一个打印语句,但将包含所有应用程序代码 ef_UI.py-一个使用wxpython的非常基本的接口 工作原理: 运行ef_Main.py它将导入接

我对wxpython和python本身有点陌生,对于这个基本的问题我很抱歉

我正试图以一种更简单的方式组织代码来管理它。我创建了一个简单的例子来解决我的问题。基本上,它只是一个带有按钮的窗口,用于打印消息。我把它分为三个简单的包

ef_Main.py-这是它将导入UI和应用程序本身的主要包

ef_Tool.py-应用程序将运行所有重要的代码,现在它只是一个打印语句,但将包含所有应用程序代码

ef_UI.py-一个使用wxpython的非常基本的接口

工作原理:

运行ef_Main.py它将导入接口(ef_UI.py)和主代码(ef_Tool.py)。当在界面中单击某个内容时,ef_Main将准备就绪,并发送到ef_工具以执行

我的问题是:

我不知道如何使用bind函数连接这三个包。我相信它应该在ef_Main中,但是它将如何从界面获取信息并将其发送到ef_Tool.py

如果我想从ef_工具获得一些输出并将其发送回接口。我该怎么做呢

这是我的密码

#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))
=======================

#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

以下是一个满足您需求的简单示例。假设所有应用程序逻辑都在
ef_-Tool.py
中完成,这些逻辑的输入来自
ef_-UI
,输出也发送到
ef_-UI.py

ef\u UI
中发生按钮单击事件时,需要在
ef\u Tool.py
中调用一个方法。您可以从
MyFrame
的方法调用此方法。但要做到这一点,您需要一个对象
MyTools

因此,首先,在
ef_Main.py
中为
MyTools
创建一个对象,并将该对象传递给
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的对象存储在MyFrame类中。然后使用此对象调用
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
希望这有帮助