您必须为按钮单击添加事件处理程序。把这个添加到你的窗口初始化。(按钮应与xaml代码上的按钮名称匹配)ui = wpf.LoadComponent(self, 'WpfApplication1.xaml')
ui.BUTTON.Click += self.Button_Click
也可以通过xaml代码实现相同的效果:
^{pr2}$
带有以下注释的工作代码:import wpf
from System.Windows import Application, Window
class MyWindow(Window):
def __init__(self):
self.ui = wpf.LoadComponent(self, 'form.xaml')
# not needed because event handler
# is in XAML
# to handle event on code, remove this from xaml's button tag:
# Click="Button_Click"
# and uncomment line below:
# self.ui.Button.Click += self.Button_Click
def Button_Click(self, sender, e):
print('Button has clicked')
if __name__ == '__main__':
Application().Run(MyWindow())
# Alternatively, below also works:
# form = MyWindow()
# form.ShowDialog()
见工作表截图: