import wx
class MyFrame(wx.Frame):
def __init__(self, parent, title):
super(MyFrame, self).__init__(parent, title=title, size=(400, 300))
# 创建面板
panel = wx.Panel(self)
# 创建按钮
button = wx.Button(panel, label="Click Me", pos=(150, 120))
button.Bind(wx.EVT_BUTTON, self.on_button_click)
# 设置窗口居中显示
self.Centre()
def on_button_click(self, event):
wx.MessageBox("Button clicked!", "Info", wx.OK | wx.ICON_INFORMATION)
class MyApp(wx.App):
def OnInit(self):
frame = MyFrame(None, "wxPython Example")
frame.Show(True)
return True
if __name__ == "__main__":
app = MyApp()
app.MainLoop()
wx
模块,这是使用 wxPython 所必需的。MyFrame
:wx.Frame
,用于创建应用程序的主窗口。panel
,所有控件都将放置在这个面板上。button
,并绑定点击事件到 on_button_click
方法。self.Centre()
方法使窗口居中显示。on_button_click
:MyApp
:wx.App
,在 OnInit
方法中创建并显示主窗口。MyApp
实例并启动主事件循环。这段代码展示了如何使用 wxPython 创建一个简单的 GUI 应用程序,包含一个按钮和一个消息框。
上一篇:python后缀
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站