如何用自己调用其他 class 的函数?
How to call function from other class with self?
一个 post 就在我之前,我找到了一些我想使用的代码。有一个 ComboPopup 里面有复选框。如果激活了这些复选框之一,我想将所选文本传回我的 class(即 MyForm
)。有一个名为 self.text
的 StaticText。我想用 ComboPopup 的选择文本更改标签。
我试过:
test = MyForm()
MyForm.OnUpdate(test,item.GetText())
因为我认为 self.text
是 MyForm()
的父级。但这是行不通的。没有错误,但文本也没有变化。
在这种情况下 self
是什么?有什么好方法可以找出 self
是什么吗?比如打印名称或任何东西:-)
我的代码:
import wx
import wx.stc
from wx.lib.mixins.listctrl import CheckListCtrlMixin, ListCtrlAutoWidthMixin
class CheckListCtrl(wx.ListCtrl, CheckListCtrlMixin, ListCtrlAutoWidthMixin):
def __init__(self, parent):
wx.ListCtrl.__init__(self, parent, wx.ID_ANY, style=wx.LC_REPORT |
wx.SUNKEN_BORDER)
CheckListCtrlMixin.__init__(self)
ListCtrlAutoWidthMixin.__init__(self)
self.SetSize(-1, -1, -1, 50)
def OnCheckItem(self, index, flag):
item = self.GetItem(index)
if flag:
what = "checked"
else:
what = "unchecked"
print(f'{item.GetText()} - {what}')
test = MyForm()
MyForm.OnUpdate(test,item.GetText())
class ListViewComboPopup(wx.ComboPopup):
def __init__(self):
wx.ComboPopup.__init__(self)
self.lc = None
def AddItem(self, txt):
self.lc.InsertItem(0, txt)
def Init(self):
self.value = -1
self.curitem = -1
def Create(self, parent):
self.lc = CheckListCtrl(parent)
self.lc.InsertColumn(0, '', width=90)
return True
def GetControl(self):
return self.lc
def OnPopup(self):
wx.ComboPopup.OnPopup(self)
def GetAdjustedSize(self, minWidth, prefHeight, maxHeight):
return wx.ComboPopup.GetAdjustedSize(
self, minWidth, 110, maxHeight)
class MyForm(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, title="Popup Menu")
self.panel = wx.Panel(self)
vsizer = wx.BoxSizer(wx.VERTICAL)
comboCtrl = wx.ComboCtrl(self.panel, wx.ID_ANY, "Select Text")
popupCtrl = ListViewComboPopup()
comboCtrl.SetPopupControl(popupCtrl)
popupCtrl.AddItem("Text One")
self.txt = wx.StaticText(self.panel,-1,style = wx.ALIGN_LEFT)
self.txt.SetLabel("Startup Text")
vsizer.Add(comboCtrl,1,wx.EXPAND)
vsizer.Add(self.txt,1,wx.EXPAND)
self.panel.SetSizer(vsizer)
def OnUpdate(self, txt):
self.txt.SetLabel(txt)
if __name__ == "__main__":
app = wx.App(False)
frame = MyForm().Show()
app.MainLoop()
我不知道 wx 库是做什么的,但有一种方法可以检查 .text 在哪里。
You want vars() mixed with pprint():
from pprint import pprint
pprint(vars(your_object))
pprint(your_object) # this is OK too
建议 2
type(x).__name__
这将为您提供实例的 class 名称。您可以在 self.text 之前插入此行。并将 self 作为参数而不是 x.
原文:Link
您的 wx.Frame
子 class 实例没有父实例。您在没有一个的情况下明确创建它:
wx.Frame.__init__(self, None, title="Popup Menu")
您在 __name__ == '__main__'
块中创建了 MyForm
的实例:
frame = MyForm().Show()
# Note: your name 'frame' holds the return value of the method Show(), i.e. a boolean
# This probably should rather read:
# frame = MyForm()
# frame.Show()
此 是您在 app
.
中显示的 MyForm
实例
你在这里做什么:
test = MyForm()
正在创建 MyFrame
的 new 实例(这与您的应用显示的实例无关)。然后,您在 MyForm
class
的 new 实例上调用 onUpdate
MyForm.OnUpdate(test,item.GetText())
由于您从未 Show()
那个新实例,因此您看不到操作的效果。但是,您可能不会 want/need 那个新实例。
您需要来自 main
块的 您的 实例。
CheckListCtrl
初始值设定项中有一个 parent
参数。这可能包含一连串的对象,您可以提升这些对象直到到达 MyForm
实例。
我不能确定,因为它在 ListViewComboPopup
:
中是不可见的
def Create(self, parent):
self.lc = CheckListCtrl(parent)
在 OnCheckItem
中执行 print(self.Parent)
以查看其中包含的内容,然后将另一个 .Parent
添加到 self.Parent
直到您希望最终得到 <__main__.MyForm instance [...]>
.这是您要调用 onUpdate
方法的地方。这看起来应该类似于:
self.Parent.Parent.Parent.OnUpdate(item.GetText())
# the number of '.Parent' my vary, based on where in the chain you find your MyForm instance
编辑
根据 OP 的评论,wx
对象的父属性拼写为大写 P
。相应的代码片段已相应更新。
一个 post 就在我之前,我找到了一些我想使用的代码。有一个 ComboPopup 里面有复选框。如果激活了这些复选框之一,我想将所选文本传回我的 class(即 MyForm
)。有一个名为 self.text
的 StaticText。我想用 ComboPopup 的选择文本更改标签。
我试过:
test = MyForm()
MyForm.OnUpdate(test,item.GetText())
因为我认为 self.text
是 MyForm()
的父级。但这是行不通的。没有错误,但文本也没有变化。
在这种情况下 self
是什么?有什么好方法可以找出 self
是什么吗?比如打印名称或任何东西:-)
我的代码:
import wx
import wx.stc
from wx.lib.mixins.listctrl import CheckListCtrlMixin, ListCtrlAutoWidthMixin
class CheckListCtrl(wx.ListCtrl, CheckListCtrlMixin, ListCtrlAutoWidthMixin):
def __init__(self, parent):
wx.ListCtrl.__init__(self, parent, wx.ID_ANY, style=wx.LC_REPORT |
wx.SUNKEN_BORDER)
CheckListCtrlMixin.__init__(self)
ListCtrlAutoWidthMixin.__init__(self)
self.SetSize(-1, -1, -1, 50)
def OnCheckItem(self, index, flag):
item = self.GetItem(index)
if flag:
what = "checked"
else:
what = "unchecked"
print(f'{item.GetText()} - {what}')
test = MyForm()
MyForm.OnUpdate(test,item.GetText())
class ListViewComboPopup(wx.ComboPopup):
def __init__(self):
wx.ComboPopup.__init__(self)
self.lc = None
def AddItem(self, txt):
self.lc.InsertItem(0, txt)
def Init(self):
self.value = -1
self.curitem = -1
def Create(self, parent):
self.lc = CheckListCtrl(parent)
self.lc.InsertColumn(0, '', width=90)
return True
def GetControl(self):
return self.lc
def OnPopup(self):
wx.ComboPopup.OnPopup(self)
def GetAdjustedSize(self, minWidth, prefHeight, maxHeight):
return wx.ComboPopup.GetAdjustedSize(
self, minWidth, 110, maxHeight)
class MyForm(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, title="Popup Menu")
self.panel = wx.Panel(self)
vsizer = wx.BoxSizer(wx.VERTICAL)
comboCtrl = wx.ComboCtrl(self.panel, wx.ID_ANY, "Select Text")
popupCtrl = ListViewComboPopup()
comboCtrl.SetPopupControl(popupCtrl)
popupCtrl.AddItem("Text One")
self.txt = wx.StaticText(self.panel,-1,style = wx.ALIGN_LEFT)
self.txt.SetLabel("Startup Text")
vsizer.Add(comboCtrl,1,wx.EXPAND)
vsizer.Add(self.txt,1,wx.EXPAND)
self.panel.SetSizer(vsizer)
def OnUpdate(self, txt):
self.txt.SetLabel(txt)
if __name__ == "__main__":
app = wx.App(False)
frame = MyForm().Show()
app.MainLoop()
我不知道 wx 库是做什么的,但有一种方法可以检查 .text 在哪里。
You want vars() mixed with pprint():
from pprint import pprint pprint(vars(your_object)) pprint(your_object) # this is OK too
建议 2
type(x).__name__
这将为您提供实例的 class 名称。您可以在 self.text 之前插入此行。并将 self 作为参数而不是 x.
原文:Link
您的 wx.Frame
子 class 实例没有父实例。您在没有一个的情况下明确创建它:
wx.Frame.__init__(self, None, title="Popup Menu")
您在 __name__ == '__main__'
块中创建了 MyForm
的实例:
frame = MyForm().Show()
# Note: your name 'frame' holds the return value of the method Show(), i.e. a boolean
# This probably should rather read:
# frame = MyForm()
# frame.Show()
此 是您在 app
.
中显示的 MyForm
实例
你在这里做什么:
test = MyForm()
正在创建 MyFrame
的 new 实例(这与您的应用显示的实例无关)。然后,您在 MyForm
class
onUpdate
MyForm.OnUpdate(test,item.GetText())
由于您从未 Show()
那个新实例,因此您看不到操作的效果。但是,您可能不会 want/need 那个新实例。
您需要来自 main
块的 您的 实例。
CheckListCtrl
初始值设定项中有一个 parent
参数。这可能包含一连串的对象,您可以提升这些对象直到到达 MyForm
实例。
我不能确定,因为它在 ListViewComboPopup
:
def Create(self, parent):
self.lc = CheckListCtrl(parent)
在 OnCheckItem
中执行 print(self.Parent)
以查看其中包含的内容,然后将另一个 .Parent
添加到 self.Parent
直到您希望最终得到 <__main__.MyForm instance [...]>
.这是您要调用 onUpdate
方法的地方。这看起来应该类似于:
self.Parent.Parent.Parent.OnUpdate(item.GetText())
# the number of '.Parent' my vary, based on where in the chain you find your MyForm instance
编辑
根据 OP 的评论,wx
对象的父属性拼写为大写 P
。相应的代码片段已相应更新。