メフディAslani:
import wx
class MyPanel(wx.Panel):
def __init__(self, parent):
super().__init__(parent)
btn = wx.Button(self, label="Press me")
btn.Bind(wx.EVT_BUTTON, self.on_button_press)
def on_button_press(self, event):
print("You pressed the button")
class MyFrame(wx.Frame):
def __init__(self):
super().__init__(parent=None, title="Hello wxPython")
panel = MyPanel(self)
self.Show()
if __name__ == "__main__":
app = wx.App(redirect=False)
frame = MyFrame()
app.MainLoop()
上記のコードでは、wx.EVT_BUTTONにwx.Buttonを結合するためにbtn.Bindを使用しました。
代わりにあれば、我々はこの方法を使用します。 self.Bind(wx.EVT_BUTTON, self.on_button_press, btn)
結果は上記と同じになります。今、私の質問はself.Bindとbtn.Bindとの間の差です。
ザクセン州のロルフ:
各ウィジェットには、IDを持っています。
トリガされたときのイベントが、この場合はボタンで、トリガウィジェットのIDを渡します。
機能にイベントをバインドすると、特定または一般的なすなわちA特定のウィジェットまたは任意のウィジェットに起動そのイベントのタイプにすることができます。
要するに、この場合には、self.Bind
結合する任意のボタンイベントは、あなたは、ウィジェットIDを指定しない限り。
参照:https://docs.wxpython.org/events_overview.html
うまくいけば、意志の助け以下のコードを説明します。
NBは、event.Skip()
プロセスへのより多くのイベントがあるかどうかを確認、このイベントで停止しないと言います。
import wx
class MyPanel(wx.Panel):
def __init__(self, parent):
super().__init__(parent)
btn1 = wx.Button(self, label="Press me 1", pos=(10,10))
btn2 = wx.Button(self, label="Press me 2", pos=(10,50))
Abtn = wx.Button(self, label="Press me", pos=(10,90))
# Bind btn1 to a specific callback routine
btn1.Bind(wx.EVT_BUTTON, self.on_button1_press)
# Bind btn2 to a specific callback routine specifying its Id
# Note the order of precedence in the callback routines
self.Bind(wx.EVT_BUTTON, self.on_button2_press, btn2)
# or identify the widget via its number
# self.Bind(wx.EVT_BUTTON, self.on_button2_press, id=btn2.GetId())
# Bind any button event to a callback routine
self.Bind(wx.EVT_BUTTON, self.on_a_button_press)
# button 1 pressed
def on_button1_press(self, event):
print("You pressed button 1")
event.Skip()
# button 2 pressed
def on_button2_press(self, event):
print("You pressed button 2")
event.Skip()
# Any button pressed
def on_a_button_press(self, event):
print("You pressed a button")
event.Skip()
class MyFrame(wx.Frame):
def __init__(self):
super().__init__(parent=None, title="Hello wxPython")
panel = MyPanel(self)
self.Show()
if __name__ == "__main__":
app = wx.App(redirect=False)
frame = MyFrame()
app.MainLoop()