您必须为按钮单击添加事件处理程序。把这个添加到你的窗口初始化。(按钮应与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()
见工作表截图:
本文介绍如何在WPF应用程序中为按钮添加点击事件处理程序。提供了使用Python结合wpf库实现按钮点击事件的示例代码,并展示了如何通过XAML和代码两种方式设置事件处理程序。
16万+

被折叠的 条评论
为什么被折叠?



