📅  最后修改于: 2023-12-03 15:28:06.792000             🧑  作者: Mango
wxPython 是一种基于Python编程语言的GUI开发框架,它使用了与主流桌面应用程序的外观和行为相似的控件,以及跨平台的解决方案,方便程序员创建富有表现力的GUI应用程序。
import wx
class MyFrame(wx.Frame):
def __init__(self):
super().__init__(None, title="My Window", size=(300,200))
self.Center()
if __name__ == "__main__":
app = wx.App()
frame = MyFrame()
frame.Show()
app.MainLoop()
import wx
class MyFrame(wx.Frame):
def __init__(self):
super().__init__(None, title="My Window", size=(300,200))
self.Center()
panel = wx.Panel(self)
# 添加文本框和按钮
self.textCtrl = wx.TextCtrl(panel, pos=(20,20))
self.button = wx.Button(panel, label="Click Me", pos=(20,50))
# 绑定事件
self.Bind(wx.EVT_BUTTON, self.onButton, self.button)
def onButton(self, event):
print(self.textCtrl.GetValue())
if __name__ == "__main__":
app = wx.App()
frame = MyFrame()
frame.Show()
app.MainLoop()
import wx
class MyFrame(wx.Frame):
def __init__(self):
super().__init__(None, title="My Window", size=(300,200))
self.Center()
panel = wx.Panel(self)
# 水平布局
hbox = wx.BoxSizer(wx.HORIZONTAL)
staticText = wx.StaticText(panel, label="Username:")
textbox = wx.TextCtrl(panel)
hbox.Add(staticText, flag=wx.RIGHT, border=10)
hbox.Add(textbox, proportion=1)
# 垂直布局
vbox = wx.BoxSizer(wx.VERTICAL)
vbox.Add(hbox, flag=wx.EXPAND|wx.LEFT|wx.RIGHT|wx.TOP, border=10)
button = wx.Button(panel, label="Login", size=(70,30))
vbox.Add(button, flag=wx.ALIGN_CENTER|wx.TOP|wx.BOTTOM, border=10)
panel.SetSizer(vbox) # 设置布局管理器
if __name__ == "__main__":
app = wx.App()
frame = MyFrame()
frame.Show()
app.MainLoop()
import wx
class MyCtrl(wx.Control):
def __init__(self, parent, ID=wx.ID_ANY, label="", pos=wx.DefaultPosition,
size=wx.DefaultSize, style=wx.NO_BORDER,
validator=wx.DefaultValidator, name="myctrl"):
super().__init__(parent, ID, pos, size, style, validator, name)
self.label = label
self.Bind(wx.EVT_PAINT, self.OnPaint)
self.SetInitialSize(size)
def OnPaint(self, event):
dc = wx.PaintDC(self)
font = wx.Font(18, wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_NORMAL)
dc.SetFont(font)
dc.DrawText(self.label, 0, 0)
class MyFrame(wx.Frame):
def __init__(self):
super().__init__(None, title="My Window", size=(300,200))
self.Center()
panel = wx.Panel(self)
# 添加自定义控件
myctrl = MyCtrl(panel, label="Hello, World!", size=(150,40), pos=(20,20))
if __name__ == "__main__":
app = wx.App()
frame = MyFrame()
frame.Show()
app.MainLoop()
wxPython 是一种功能强大、易于使用的GUI开发框架,适合广大Python程序员使用。通过学习wxPython,可以快速开发出跨平台的GUI应用程序,帮助各种行业提高工作效率,实现更多有意义的事情。