python调用.a静态库_Python:在B类中从A类调用方法A?

There are a number of questions that are similar to this, but none of the answers hits the spot - so please bear with me.

I am trying my hardest to learn OOP using Python, but i keep running into errors (like this one) which just make me think this is all pointless and it would be easier to just use methods.

Here is my code:

class TheGUI(wx.Frame):

def __init__(self, title, size):

wx.Frame.__init__(self, None, 1, title, size=size)

# The GUI is made ...

textbox.TextCtrl(panel1, 1, pos=(67,7), size=(150, 20))

button1.Bind(wx.EVT_BUTTON, self.button1Click)

self.Show(True)

def button1Click(self, event):

#It needs to do the LoadThread function!

class WebParser:

def LoadThread(self, thread_id):

#It needs to get the contents of textbox!

TheGUI = TheGUI("Text RPG", (500,500))

TheParser = WebParser

TheApp.MainLoop()

So the problem i am having is that the GUI class needs to use a function that is in the WebParser class, and the WebParser class needs to get text from a textbox that exists in the GUI class.

I know i could do this by passing the objects around as parameters, but that seems utterly pointless, there must be a more logical way to do this that doesn't using classes seem so pointless?

Thanks in advance!

解决方案

You seem to have several misconceptions.

You are using methods here. (Did you mean "functions", as in not belonging to a class?)

Classes are not some kind of container or namespace for functionality. They define a data type. The point of having the class WebParser is that you can have more than one WebParser.

In Python, classes are objects too. Right now, you aren't making TheParser be a WebParser; you are making it an alias for the class itself. In other words, TheParser is now equal to "the concept of a web parser", more or less.

Passing parameters around is not "pointless"; it's how you get information between functions. You don't (normally!) pass classes around; you pass around instances of the classes. Whether you use classes or not, you will have fundamentally the same communication problem.

It is very simple to fix this. First off, the WebParser needs to be created, so we take care of that by using its constructor. Next, our instance of TheGUI (a poor name btw - "the" doesn't really belong) needs to know about our instance, so we pass it to the constructor. That way, the instance can keep a reference to it, and use it to call the LoadThread method. It also needs to keep a reference to the textbox, so that this information can be passed along to LoadThread.

It looks like:

class MyGUI(wx.Frame):

def __init__(self, title, size, loader):

wx.Frame.__init__(self, None, 1, title, size=size)

# The GUI is made ...

textbox.TextCtrl(panel1, 1, pos=(67,7), size=(150, 20))

self.textbox = textbox

button1.Bind(wx.EVT_BUTTON, self.button1Click)

self.loader = loader

self.Show(True)

def button1Click(self, event):

self.loader.LoadThread(get_thread_id(), self.textbox)

class WebParser:

def LoadThread(self, thread_id, a_textbox):

do_something_with(a_textbox)

TheGUI = MyGUI("Text RPG", (500,500), WebParser())

TheApp.MainLoop()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值