Python| wxPython模块介绍
Python提供了wxpython 模块,它允许我们创建功能强大的图形用户界面。它是一个开源模块,这意味着任何人都可以免费使用它,并且任何人都可以查看和修改源代码。
它被实现为一组扩展模块,这些模块包装了用 C++ 编写的 wxWidgets 库的 GUI 组件。它是Python的跨平台GUI工具包,Phoenix版Phoenix是改进的下一代wxPython,主要关注速度、维护能力和可扩展性。
使用以下命令安装:
pip install wxpython
使用 wxpython 创建 GUI:
- 首先导入wx模块。
- 为应用程序类创建一个对象。
- 为框架类创建一个对象,并将其他控件添加到框架对象,以便使用面板维护其布局。
- 然后添加一个静态文本对象来显示 Hello World 。
- 使用 show 方法显示框架窗口。
- 使用主事件循环应用程序对象运行应用程序直到窗口关闭。
示例 #1:一个简单的 GUI 应用程序,它使用 wxpython 显示GEEKS FOR GEEKS 。
Python3
# import wx module
import wx
# creating application object
app1 = wx.App()
# creating a frame
frame = wx.Frame(None, title ="GFG")
pa = wx.Panel(frame)
# Adding a text to the frame object
text1 = wx.StaticText(pa, label ="GEEKS FOR GEEKS", pos =(100, 50))
# show it
frame.Show()
# start the event loop
app1.Mainloop()
Python3
# Import wx module
import wx
# creating application object
app1 = wx.App()
# creating a frame
frame = wx.Frame(None, title ="wxpython app")
pa = wx.Panel(frame)
# Button creation
e = wx.Button(pa, -1, "Button1", pos = (120, 100))
# show it
frame.Show()
# start the event loop
app1.Mainloop()
Python3
# importing wx module
import wx
# creating application object
app1 = wx.App()
# creating a frame
frame = wx.Frame(None, title ="wxpython app")
pa = wx.Panel(frame)
# Checkbox creation using wx module
e = wx.CheckBox(pa, -1, "CheckBox1", pos = (120, 100))
e = wx.CheckBox(pa, -1, "CheckBox2", pos = (120, 120))
# show it
frame.Show()
# start the event loop
app1.Mainloop()
Python3
# importing wx module
import wx
# creating application object
app1 = wx.App()
# creating a frame
frame = wx.Frame(None, title ="wxpython app")
pa = wx.Panel(frame)
# RadioButton creation using wx module
e = wx.RadioButton(pa, -1, "RadioButton1", pos = (120, 100))
e = wx.RadioButton(pa, -1, "radioButton2", pos = (120, 120))
# show it
frame.Show()
# start the event loop
app1.Mainloop()
输出:
示例 #2:使用 wx 模块创建按钮
Python3
# Import wx module
import wx
# creating application object
app1 = wx.App()
# creating a frame
frame = wx.Frame(None, title ="wxpython app")
pa = wx.Panel(frame)
# Button creation
e = wx.Button(pa, -1, "Button1", pos = (120, 100))
# show it
frame.Show()
# start the event loop
app1.Mainloop()
输出:
Example #3: CheckBox 使用 wxpython
Python3
# importing wx module
import wx
# creating application object
app1 = wx.App()
# creating a frame
frame = wx.Frame(None, title ="wxpython app")
pa = wx.Panel(frame)
# Checkbox creation using wx module
e = wx.CheckBox(pa, -1, "CheckBox1", pos = (120, 100))
e = wx.CheckBox(pa, -1, "CheckBox2", pos = (120, 120))
# show it
frame.Show()
# start the event loop
app1.Mainloop()
输出:
示例 #4:使用 wxpython 的 RadioButtons
Python3
# importing wx module
import wx
# creating application object
app1 = wx.App()
# creating a frame
frame = wx.Frame(None, title ="wxpython app")
pa = wx.Panel(frame)
# RadioButton creation using wx module
e = wx.RadioButton(pa, -1, "RadioButton1", pos = (120, 100))
e = wx.RadioButton(pa, -1, "radioButton2", pos = (120, 120))
# show it
frame.Show()
# start the event loop
app1.Mainloop()
输出: