【脚本语言系列】关于PythonGUI编程wxPython, 你需要知道的事

时间:2022-06-17 22:58:52

如何使用wxPython

  1. 创建简单窗口
# -*- coding:utf-8 -*-
#
import wx
app = wx.App(0)
frame = wx.Frame(None)
frame = wx.Frame(parent = None, title = "Hello, wxPython!")
frame.Show(True)
app.MainLoop()

【脚本语言系列】关于PythonGUI编程wxPython, 你需要知道的事
【脚本语言系列】关于PythonGUI编程wxPython, 你需要知道的事
2. 创建窗口

# -*- coding:utf-8 -*-
#
import wx
class MyApp(wx.App):
    def OnInit(self):
        frame = wx.Frame(parent = None, title = "Hello, wxPython!")
        frame.Show()
        return True
app = MyApp()
app.MainLoop()

【脚本语言系列】关于PythonGUI编程wxPython, 你需要知道的事
【脚本语言系列】关于PythonGUI编程wxPython, 你需要知道的事
3. 组件
* 面板

# -*- coding:utf-8 -*-
#
import wx
class MyApp(wx.App):
    def OnInit(self):
        frame = wx.Frame(parent = None,
                 id = -1,
                 title = "Panel",
                 pos=(100, 100),
                 size=(600, 480),
                 style = wx.DEFAULT_FRAME_STYLE,
                 name = "frame")
        panel = wx.Panel(frame, -1)
        frame.Show()
        return True
app = MyApp()
app.MainLoop()

【脚本语言系列】关于PythonGUI编程wxPython, 你需要知道的事
【脚本语言系列】关于PythonGUI编程wxPython, 你需要知道的事
* 按钮

# -*- coding:utf-8 -*-
#
import wx
class MyApp(wx.App):
    def OnInit(self):
        frame = wx.Frame(parent = None, title = 'Button')
        panel = wx.Panel(frame, -1)
        button = wx.Button(panel,
                -1,
                "Button",
                pos=(50,50))
        frame.Show()
        return True
app = MyApp()
app.MainLoop()

【脚本语言系列】关于PythonGUI编程wxPython, 你需要知道的事
【脚本语言系列】关于PythonGUI编程wxPython, 你需要知道的事
* 标签

# -*- coding:utf-8 -*-
#
import wx
class MyApp(wx.App):
    def OnInit(self):
        frame = wx.Frame(parent = None, title = 'wxPython',size=(300,200))
        panel = wx.Panel(frame, -1)
        label1 = wx.StaticText(panel,
                -1,
                "Python",
                size=(160, 20),
                pos = (60, 10),
                style = wx.ALIGN_RIGHT)
        label2 = wx.StaticText(panel,
                -1,
                "Python",
                size=(160, 20),
                pos = (60, 50),
                style = wx.ALIGN_CENTER) 
        label2.SetForegroundColour('red')
        label2.SetBackgroundColour('black')
        label3 = wx.StaticText(panel,
                -1,
                'Python\nwxPython',
                size = (160, 40),
                pos = (60, 90))
        frame.Show()
        return True
app = MyApp()
app.MainLoop()

【脚本语言系列】关于PythonGUI编程wxPython, 你需要知道的事
【脚本语言系列】关于PythonGUI编程wxPython, 你需要知道的事
* 文本框
* 单行文本框

# -*- coding:utf-8 -*-
#
import wx
class MyApp(wx.App):
    def OnInit(self):
        frame = wx.Frame(parent = None, title = 'wxPython',size=(300,200))
        panel = wx.Panel(frame, -1)
        label1 = wx.StaticText(panel, -1, "wxPython", pos = (120, 20))
        label2 = wx.StaticText(panel, -1, "User Name:", pos = (10, 50)) 

        text = wx.TextCtrl(panel,
                -1,
                "Python",
                size=(100, 20),
                pos = (120, 50)) 
        label3 = wx.StaticText(panel, -1, "Password:", pos = (10, 100))
        password = wx.TextCtrl(panel,
                -1,
                "password",
                size=(100, 20),
                pos = (120, 100),
                style = wx.TE_PASSWORD) 
        frame.Show()
        return True
app = MyApp()
app.MainLoop()

【脚本语言系列】关于PythonGUI编程wxPython, 你需要知道的事
【脚本语言系列】关于PythonGUI编程wxPython, 你需要知道的事
* 多行文本框

# -*- coding:utf-8 -*-
#
import wx
class MyApp(wx.App):
    def OnInit(self):
        frame = wx.Frame(parent = None, title = 'wxPython',size=(600,400))
        panel = wx.Panel(frame, -1)
        label1 = wx.StaticText(panel, -1, "MultiLine", pos = (280, 10))
        text1 = wx.TextCtrl(panel,
                -1,
                "Python",
                pos =(10, 30),
                size = (560, 150),
                style = wx.TE_MULTILINE) 
        label2 = wx.StaticText(panel, -1, "RichText", pos = (280, 190))
        text2 = wx.TextCtrl(panel,
                -1,
                "Python wxPython",
                pos=(10, 210),
                size = (560, 150),
                style = wx.TE_MULTILINE|wx.TE_RICH) 
        text2.SetStyle(0,6,wx.TextAttr('red','blue'))
        frame.Show()
        return True
app = MyApp()
app.MainLoop()

【脚本语言系列】关于PythonGUI编程wxPython, 你需要知道的事
【脚本语言系列】关于PythonGUI编程wxPython, 你需要知道的事
* 单选框和复选框

# -*- coding:utf-8 -*-
#
import wx
class MyApp(wx.App):
    def OnInit(self):
        frame = wx.Frame(parent = None, title = 'wxPython',size=(300,200))
        panel = wx.Panel(frame, -1)
        self.radio1 = wx.RadioButton(panel, -1, "Radio1", pos = (10, 40), style = wx.RB_GROUP)
        self.radio2 = wx.RadioButton(panel, -1, "Radio2", pos = (10, 80))
        self.radio3 = wx.RadioButton(panel, -1, "Radio3", pos = (10, 120))
        self.check = wx.CheckBox(panel, -1, "CheckBox", pos = (120, 40), size = (150, 20))
        self.button1 = wx.Button(panel, -1, "Radio", pos = (120, 80))
        self.button2 = wx.Button(panel, -1, "Check", pos = (120, 120))
        self.Bind(wx.EVT_BUTTON, self.OnButton1, self.button1)
        self.Bind(wx.EVT_BUTTON, self.OnButton2, self.button2)
        frame.Show()
        return True
    def OnButton1(self, event):
        if self.radio1.GetValue():
            self.buton1.SetLabel("Radio1")
        elif self.radio2.GetValue():
            self.button1.SetLabel("Radio2")
        else:
            self.button1.SetLabel("Radio3")
    def OnButton2(self, event):
        if self.check.IsChecked():
            self.button2.SetLabel("Checked")
        else:
            self.button2.SetLabel("UnChecked")
app = MyApp()
app.MainLoop()

【脚本语言系列】关于PythonGUI编程wxPython, 你需要知道的事
【脚本语言系列】关于PythonGUI编程wxPython, 你需要知道的事
* 使用sizer布置组件
4. 对话框
* 消息框

# -*- coding:utf-8 -*-
#
import wx
class MyApp(wx.App):
    def OnInit(self):
        self.frame = wx.Frame(parent = None, title = 'wxPython',size=(300,200))
        panel = wx.Panel(self.frame, -1)
        self.button1 = wx.Button(panel, -1, 'Style1', pos=(100, 20))
        self.button2 = wx.Button(panel, -1, 'Style2', pos=(100, 50))
        self.button3 = wx.Button(panel, -1, 'Style3', pos=(100, 80))
        self.button4 = wx.Button(panel, -1, 'Style4', pos=(100, 110))
        self.button5 = wx.Button(panel, -1, 'Style5', pos=(100, 140))
        self.Bind(wx.EVT_BUTTON, self.OnButton1,self.button1)
        self.Bind(wx.EVT_BUTTON, self.OnButton2,self.button2)
        self.Bind(wx.EVT_BUTTON, self.OnButton3,self.button3)
        self.Bind(wx.EVT_BUTTON, self.OnButton4,self.button4)
        self.Bind(wx.EVT_BUTTON, self.OnButton5,self.button5)
        self.frame.Show()
        return True
    def OnButton1(self, event):
        wx.MessageBox('Style1','wxPython',wx.YES_NO|wx.ICON_QUESTION)
    def OnButton2(self, event):
        wx.MessageBox('Style2','wxPython',wx.OK|wx.CANCEL|wx.ICON_ERROR)
    def OnButton3(self, event):
        wx.MessageBox('Style3','wxPython',wx.OK|wx.CANCEL|wx.ICON_EXCLAMATION)
    def OnButton4(self, event):
        wx.MessageBox('Style4','wxPython',wx.YES_NO|wx.NO_DEFAULT|wx.ICON_HAND) 
    def OnButton5(self, event):
        wx.MessageBox('Style5','wxPython',wx.YES_NO|wx.YES_DEFAULT|wx.ICON_INFORMATION) 
app = MyApp()
app.MainLoop()

【脚本语言系列】关于PythonGUI编程wxPython, 你需要知道的事
【脚本语言系列】关于PythonGUI编程wxPython, 你需要知道的事
【脚本语言系列】关于PythonGUI编程wxPython, 你需要知道的事
【脚本语言系列】关于PythonGUI编程wxPython, 你需要知道的事
【脚本语言系列】关于PythonGUI编程wxPython, 你需要知道的事
【脚本语言系列】关于PythonGUI编程wxPython, 你需要知道的事
【脚本语言系列】关于PythonGUI编程wxPython, 你需要知道的事
* 标准对话框

# -*- coding:utf-8 -*-
#
import wx
class MyApp(wx.App):
    def OnInit(self):
        self.frame = wx.Frame(parent = None, title = 'wxPython',size=(300,200))
        panel = wx.Panel(self.frame, -1)
        self.button1 = wx.Button(panel, -1, 'Input String', pos=(100, 20))
        self.button2 = wx.Button(panel, -1, 'Input Password', pos=(100, 70))
        self.button3 = wx.Button(panel, -1, 'Input Number', pos=(100, 120))
        self.Bind(wx.EVT_BUTTON, self.OnButton1,self.button1)
        self.Bind(wx.EVT_BUTTON, self.OnButton2,self.button2)
        self.Bind(wx.EVT_BUTTON, self.OnButton3,self.button3)
        self.frame.Show()
        return True
    def OnButton1(self, event):
        r = wx.GetTextFromUser('wxPython','String', 'Default')
        wx.MessageBox(r,"wxPython",wx.OK)
    def OnButton2(self, event):
        r = wx.GetPasswordFromUser('wxPython', 'Password')
        wx.MessageBox(r,"wxPython",wx.OK)
    def OnButton3(self, event):
        r = wx.GetNumberFromUser('Input Number','Number', 'wxPython',80)
        wx.MessageBox(str(r),"wxPython",wx.OK)
app = MyApp()
app.MainLoop()

【脚本语言系列】关于PythonGUI编程wxPython, 你需要知道的事
【脚本语言系列】关于PythonGUI编程wxPython, 你需要知道的事
【脚本语言系列】关于PythonGUI编程wxPython, 你需要知道的事
【脚本语言系列】关于PythonGUI编程wxPython, 你需要知道的事
【脚本语言系列】关于PythonGUI编程wxPython, 你需要知道的事
* 自定义对话框

# -*- coding:utf-8 -*-
#
import wx
class MyApp(wx.App):
    def OnInit(self):
        self.frame = wx.Frame(parent = None, title = 'wxPython',size=(300,170))
        panel = wx.Panel(self.frame, -1)
        self.button = wx.Button(panel, -1, 'Show Dialog', pos=(100, 50))
        self.Bind(wx.EVT_BUTTON, self.OnButton,self.button)
        self.frame.Show()
        return True
    def OnButton(self, event):
        dialog = MyDialog()
        r = dialog.ShowModal()
        if r == wx.ID_OK:
            wx.MessageBox("You input:"+dialog.text.GetValue(),"wxPython",wx.OK)
class MyDialog(wx.Dialog):
    def __init__(self):
        wx.Dialog.__init__(self, None, -1, 'wxDialog', size=(300,170))
        label = wx.StaticText(self, -1, 'Simple Dialog', pos = (120, 20))
        self.text = wx.TextCtrl(self, -1, pos = (100, 50), size = (160, -1))
        self.ok = wx.Button(self, wx.ID_OK, "OK", pos=(50, 80))
        self.cancel = wx.Button(self, wx.ID_CANCEL, "Cancel", pos=(200, 80))
app = MyApp()
app.MainLoop()

【脚本语言系列】关于PythonGUI编程wxPython, 你需要知道的事
【脚本语言系列】关于PythonGUI编程wxPython, 你需要知道的事
【脚本语言系列】关于PythonGUI编程wxPython, 你需要知道的事
5. 菜单
* 普通菜单

# -*- coding:utf-8 -*-
#
import wx
class MyApp(wx.App):
    def OnInit(self):
        frame = wx.Frame(parent = None, title = 'wxPython',size=(300,200))
        panel = wx.Panel(frame, -1)
        menuBar = wx.MenuBar()
        menu =wx.Menu()
        open = menu.Append(-1, 'Open')
        exit = menu.Append(-1, 'Save')
        menu.AppendSeparator()
        close = menu.Append(-1, 'Close')
        menuBar.Append(menu, '&File')
        menu = wx.Menu()
        copy = menu.Append(-1, 'Copy')
        paste = menu.Append(-1, 'Paste')
        cut = menu.Append(-1, 'Cut')
        menu.AppendSeparator()
        selectall = menu.Append(-1, 'SelectAll')
        menuBar.Append(menu, '&Edit')
        menu = wx.Menu()
        about = menu.Append(-1, 'About')
        menuBar.Append(menu, '&Help')
        frame.SetMenuBar(menuBar)
        frame.Show()
        return True
    def OnButton1(self, event):
        r = wx.GetTextFromUser('wxPython','String', 'Default')
        wx.MessageBox(r,"wxPython",wx.OK)
    def OnButton2(self, event):
        r = wx.GetPasswordFromUser('wxPython', 'Password')
        wx.MessageBox(r,"wxPython",wx.OK)
    def OnButton3(self, event):
        r = wx.GetNumberFromUser('Input Number','Number', 'wxPython',80)
        wx.MessageBox(str(r),"wxPython",wx.OK)
app = MyApp()
app.MainLoop()

【脚本语言系列】关于PythonGUI编程wxPython, 你需要知道的事
【脚本语言系列】关于PythonGUI编程wxPython, 你需要知道的事
* 弹出式菜单

# -*- coding:utf-8 -*-
#
import wx
class MyApp(wx.App):
    def OnInit(self):
        frame = wx.Frame(parent = None, title = 'wxPython',size=(300,200))
        self.panel = wx.Panel(frame, -1)
        menuBar = wx.MenuBar()
        self.menu =wx.Menu()
        open = self.menu.Append(-1, 'Open')
        exit = self.menu.Append(-1, 'Save')
        self.menu.AppendSeparator()
        close = self.menu.Append(-1, 'Close')
        menuBar.Append(self.menu, '&File')
        frame.SetMenuBar(menuBar)
        self.Bind(wx.EVT_RIGHT_DOWN, self.OnRClick)
        frame.Show()
        return True
    def OnRClick(self, event):
        pos = (event.GetX(), event.GetY())
        self.panel.PopupMenu(self.menu, pos)
app = MyApp()
app.MainLoop()

【脚本语言系列】关于PythonGUI编程wxPython, 你需要知道的事
【脚本语言系列】关于PythonGUI编程wxPython, 你需要知道的事
* 绑定菜单事件

# -*- coding:utf-8 -*-
#
import wx
class MyApp(wx.App):
    def OnInit(self):
        self.frame = wx.Frame(parent = None, title = 'wxPython',size=(300,200))
        self.panel = wx.Panel(self.frame, -1)
        menuBar = wx.MenuBar()
        self.menu =wx.Menu()
        open = self.menu.Append(-1, 'Open')
        save = self.menu.Append(-1, 'Save')
        self.menu.AppendSeparator()
        close = self.menu.Append(-1, 'Close')
        menuBar.Append(self.menu, '&File')
        self.menu = wx.Menu()
        about = self.menu.Append(-1, 'About')
        menuBar.Append(self.menu, '&Help')
        self.frame.SetMenuBar(menuBar)
        self.Bind(wx.EVT_MENU, self.OnOpen, open)
        self.Bind(wx.EVT_MENU, self.OnSave, save)
        self.Bind(wx.EVT_MENU, self.OnClose, close)
        self.Bind(wx.EVT_MENU, self.OnAbout, about)
        self.Bind(wx.EVT_RIGHT_DOWN, self.OnRClick)
        self.frame.Show()
        return True
    def OnOpen(self, event):
        dialog = wx.FileDialog(None, 'wxPython', style = wx.OPEN)
        dialog.ShowModal()
        dialog.Destroy()
    def OnSave(self, event):
        dialog = wx.FileDialog(None, 'wxPython', style = wx.SAVE)
        dialog.ShowModal()
        dialog.Destroy()
    def OnClose(self, event):
        self.frame.Destroy()
    def OnAbout(self, event):
        wx.MessageBox('wxPython Menu Event', 'wxPython', wx.OK)
    def OnRClick(self, event):
        pos = (event.GetX(), event.GetY())
        self.panel.PopupMenu(self.menu, pos)
app = MyApp()
app.MainLoop()

【脚本语言系列】关于PythonGUI编程wxPython, 你需要知道的事
【脚本语言系列】关于PythonGUI编程wxPython, 你需要知道的事
6. 资源文件

# -*- coding:utf-8 -*-
#
import wx
from wx import xrc
class MyApp(wx.App):
    def OnInit(self):
        self.res = xrc.XmlResource('res.xrc')
        self.frame = self.res.LoadFrame(None, 'frame')
        self.panel = xrc.XRCCTRL(self.frame, 'panel')
        self.label = xrc.XRCCTRL(self.panel, 'label')
        self.text = xrc.XRCCTRL(self.panel, 'text')                        
        self.button = xrc.XRCCTRL(self.panel, 'button')
        self.Bind(wx.EVT_BUTTON, self.OnButton, self.button)
        self.frame.Show()
        return True
    def OnButton(self, event):
        wx.MessageBox('You input:'+self.text.GetValue(), 'wxPython', wx.OK)
app = MyApp()
app.MainLoop()

【脚本语言系列】关于PythonGUI编程wxPython, 你需要知道的事
【脚本语言系列】关于PythonGUI编程wxPython, 你需要知道的事

什么是wxpython

wxPython是跨平台GUI工具库wxWidgets的封装;
wxWidgets库是由C++编写的,它类似Windows的MFC。
wxWidgets提供了对多种操作系统的支持。
由于它具有良好的可移植性,wxPython也具备了跨平台的能力。