使用 wxpython 绑定包

binding packages with wxpython

我是 wxpython 和 python 的新手,很抱歉回答这个基本问题。

我正在尝试以更易于管理的方式组织我的代码。我创建了这个简单的例子来恢复我的问题。基本上它只是一个 window 和一个打印消息的按钮。我把它分成三个简单的包

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

提前致谢!

爱默生

这是一个满足您要求的简单示例。假设您的所有应用程序逻辑都在 ef_Tool.py 中完成并且这些逻辑的输入来自 ef_UI 并且输出也发送到 ef_UI.py.

需要在ef_UI中发生按钮点击事件时调用ef_Tool.py中的方法。您可以从 MyFrame 的方法中调用此方法。但是你需要一个 MyTools 的对象来做到这一点。

所以首先,在ef_Main.py中为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 的对象存储在您的 MyFrame class 中。然后用这个对象调用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

您可以在其参数中将要发送的信息传递给应用程序逻辑,并以 return 值的形式获取结果。

#ef_Tool.py

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

        resultstr = "test successful"
        return resultstr

希望对您有所帮助。