pip install wxPython
wx.CAPTION:增加标题栏 wx.DEFAULT_FRAME_STYLE:默认样式 wx.CLOSE_BOX:标题栏上显示“关闭”按钮 wx.MAXIMIZE_BOX:标题栏上显示“最大化”按钮 wx.MINIMIZE_BOX:标题栏上显示“最小化”按钮 wx.RESIZE_BORDER:边框可改变尺寸 wx.SIMPLE_BORDER:边框没有装饰 wx.SYSTEM_MENU:增加系统菜单(有“关闭”、“移动”、“改变尺寸”等功能) wx.FRAME_SHAPED:用该样式创建的框架可以使用SetShape()方法来创建一个非矩形的窗体 wx.FRAME_TOOL_WINDOW:给框架一个比正常小的标题栏,使框架看起来像一个工具框窗体
wx.TE_CENTER:控件中的文本居中。 wx.TE_LEFT:控件中的文本左对齐。默认行为。 wx.TE_NOHIDESEL:文本始终高亮显示,只适用于Windows。 wx.TE_PASSWORD:不显示所键入的文本,代替以星号显示。 wx.TE_READONLY:文本控件为只读,用户不能修改其中的文本。 wx.TE_RIGHT:控件中的文本右对齐。 wx.TE_MULTILINE: 多行文本控件 重要方法: 使用GetValue()方法获取文本框中输入的内容,使用SetValue()方法设置文本框中的文本
编写代码,通过选择单选按钮,将对应信息写入第一个TextCtrl (FrameMouse右侧)当中。如选择FrameSize,则在第一个TextCtrl 显示窗体宽和高,当选择FramePos时,则在第一个TextCtrl 显示窗体的x和y坐标位置。通过选择多选按钮AllInfo,则能在第二个多行TextCtrl中显示窗体和鼠标的全部三项信息。
# -*- coding: utf-8 -*- """ Created on 2021-12-21 上午 10:54 @author: 浅笑醉红楼.(3303295829@qq.com) @Software: PyCharm (1) create by 浅笑醉红楼. 2021-12-21 上午 10:54 """ import wx class TestCtrlFrame(wx.Frame): def __init__(self,superion): wx.Frame.__init__(self, parent=superion, title='My Frist Form') self.panel = wx.Panel(self) self.radioButtonFramePos = wx.RadioButton(self.panel, -1, 'FramePos:') self.radioButtonFrameSize = wx.RadioButton(self.panel, -1, 'FrameSize:') self.radioButtonMousePos = wx.RadioButton(self.panel, -1, 'MousePos:') self.checkBoxAllinfo = wx.CheckBox(self.panel, -1, 'AllInfo') self.label1 = wx.StaticText(self.panel, -1, 'FrameMouse', style=wx.ALIGN_LEFT) self.label2 = wx.StaticText(self.panel, -1, 'AllInfo', style=wx.ALIGN_LEFT) self.FrameMouse = wx.TextCtrl(self.panel, -1, "", style=wx.TE_READONLY) self.AllInfo = wx.TextCtrl(self.panel, -1, "", style=wx.TE_MULTILINE,size=(150,100)) sizer1 = wx.FlexGridSizer(4, 2, 5, 5) sizer1.AddMany( [(self.radioButtonFramePos, 0, wx.EXPAND), (self.radioButtonFrameSize, 0, wx.EXPAND), (self.radioButtonMousePos, 0, wx.EXPAND), (self.checkBoxAllinfo, 0, wx.EXPAND), (self.label1, 0, wx.ALIGN_RIGHT), (self.FrameMouse, 0, wx.SHAPED), (self.label2, 0, wx.ALIGN_RIGHT), (self.AllInfo, 0, wx.SHAPED)]) self.border = wx.BoxSizer(wx.VERTICAL) self.border.Add(sizer1, 0, wx.ALL, 80) self.panel.SetSizerAndFit(self.border) self.Fit() # 窗体大小自适应 self.Bind(wx.EVT_SIZE, self.OnSize) self.Bind(wx.EVT_MOVE, self.OnFrameMove) self.panel.Bind(wx.EVT_MOTION, self.OnMouseMove) def OnSize(self, event): self.size = event.GetSize() if self.radioButtonFrameSize.GetValue() == True: self.FrameMouse.SetValue("%s, %s" % (self.size.width, self.size.height)) if self.checkBoxAllinfo.GetValue() == True: self.AllInfo.SetValue("窗体大小%s, %s\n窗体位置%s, %s\n鼠标位置%s, %s" % (self.size.width, self.size.height,self.posFrameMove.x, self.posFrameMove.y,self.pos.x, self.pos.y)) def OnFrameMove(self,event): self.posFrameMove = event.GetPosition() if self.radioButtonFramePos.GetValue() == True: self.FrameMouse.SetValue("%s, %s" % (self.posFrameMove.x, self.posFrameMove.y)) if self.checkBoxAllinfo.GetValue() == True: self.AllInfo.SetValue("窗体大小%s, %s\n窗体位置%s, %s\n鼠标位置%s, %s" % (self.size.width, self.size.height,self.posFrameMove.x, self.posFrameMove.y,self.pos.x, self.pos.y)) def onm ouseMove(self, event): # 鼠标移动事件处理函数 self.pos = event.GetPosition() if self.radioButtonMousePos.GetValue() == True: self.FrameMouse.SetValue("%s, %s" % (self.pos.x, self.pos.y)) if self.checkBoxAllinfo.GetValue() == True: self.AllInfo.SetValue("窗体大小%s, %s\n窗体位置%s, %s\n鼠标位置%s, %s" % (self.size.width, self.size.height,self.posFrameMove.x, self.posFrameMove.y,self.pos.x, self.pos.y)) if __name__ == '__main__': app=wx.App() frame = TestCtrlFrame(None) frame.Show() app.MainLoop()
编写代码选择单选钮、复选框并输入文本框中要求的用户名和密码之后单击“OK”按钮会弹出消息框提示输入和选择的内容,并打开聊天窗体;单击“Cancel”按钮自动清除用户的输入,并默认将单选钮“Male”设置为选中状态。
# -*- coding: utf-8 -*- """ Created on 2021-12-21 上午 10:45 @author: 浅笑醉红楼.(3303295829@qq.com) @Software: PyCharm (1) create by 浅笑醉红楼. 2021-12-21 上午 10:45 """ import wx from CH9_GUI import ChatWND class Chat(wx.Frame): def __init__(self,superion): wx.Frame.__init__(self, parent=superion, title='wx GUI') self.panel = wx.Panel(self, wx.ID_ANY) self.panel.SetBackgroundColour('Green') self.radioButtonSexM = wx.RadioButton(self.panel, -1, 'Male') self.radioButtonSexF = wx.RadioButton(self.panel, -1, 'Female') self.checkBoxAdmin = wx.CheckBox(self.panel, -1, 'Aministrator') self.label1 = wx.StaticText(self.panel, -1, 'UserName:', style=wx.ALIGN_RIGHT) self.label2 = wx.StaticText(self.panel, -1, 'Password:', style=wx.ALIGN_RIGHT) self.textName = wx.TextCtrl(self.panel, -1) self.textPwd = wx.TextCtrl(self.panel, -1, style=wx.TE_PASSWORD) self.buttonOK = wx.Button(self.panel, -1, 'OK') self.buttonCancel = wx.Button(self.panel, -1, 'Cancel') sizer1 = wx.FlexGridSizer(1, 3, 5, 5) sizer1.AddMany( [(self.radioButtonSexM, 0, wx.EXPAND), (self.radioButtonSexF, 0, wx.EXPAND), (self.checkBoxAdmin, 0, wx.EXPAND), ]) sizer2 = wx.FlexGridSizer(3, 2, 5, 5) sizer2.AddMany( [(self.label1, 0, wx.EXPAND), (self.textName, 0, wx.EXPAND), (self.label2, 0, wx.EXPAND), (self.textPwd, 0, wx.EXPAND), (self.buttonOK, 0, wx.EXPAND), (self.buttonCancel, 0, wx.EXPAND)]) self.border = wx.BoxSizer(wx.VERTICAL) self.border.Add(sizer1, 0, wx.ALL, 15) self.border.Add(sizer2, 0, wx.ALL, 15) self.panel.SetSizerAndFit(self.border) self.Fit() # 窗体大小自适应 self.Bind(wx.EVT_BUTTON, self.OnButtonOK, self.buttonOK) self.Bind(wx.EVT_BUTTON, self.OnButtonCancel, self.buttonCancel) # def OnButtonOK(self, event): # finalStr = '' # if self.radioButtonSexM.GetValue() == True: # finalStr += 'Sex:Male\n' # elif self.radioButtonSexF.GetValue() == True: # finalStr += 'Sex:Female\n' # if self.checkBoxAdmin.GetValue() == True: # finalStr += 'Administrator\n' # if self.textName.GetValue() == 'yang' and self.textPwd.GetValue() == '123': # finalStr += 'user name and password are correct\n' # else: # finalStr += 'user name or password is incorrect\n' # wx.MessageBox(finalStr) def OnButtonCancel(self, event): self.radioButtonSexM.SetValue(True) self.radioButtonSexF.SetValue(False) self.checkBoxAdmin.SetValue(True) self.textName.SetValue('') self.textPwd.SetValue('') def OnButtonOK(self, event): finalStr = '' if self.radioButtonSexM.GetValue() == True: finalStr += 'Sex:Male\n' elif self.radioButtonSexF.GetValue() == True: finalStr += 'Sex:Female\n' if self.checkBoxAdmin.GetValue() == True: finalStr += 'Administrator\n' if self.textName.GetValue() == 'yang' and self.textPwd.GetValue() == '123': finalStr += 'user name and password are correct\n' wx.MessageBox(finalStr) frame = ChatWND.ChatWND(None) # 创建框架类对象 frame.Show(True) self.Destroy() else: finalStr += 'user name or password is incorrect\n' wx.MessageBox(finalStr) if __name__ == '__main__': app=wx.App() frame = Chat(None) frame.Show() app.MainLoop()
聊天界面设计(ChatWND.py)
# -*- coding: utf-8 -*- """ Created on 2021-12-21 上午 10:54 @author: 浅笑醉红楼.(3303295829@qq.com) @Software: PyCharm (1) create by 浅笑醉红楼. 2021-12-21 上午 10:54 """ import wx class ChatWND(wx.Frame): def __init__(self, superior): wx.Frame.__init__(self, parent=superior,title=u'Chat Window', size=(800, 600)) self.panel = wx.Panel(self, wx.ID_ANY) self.panel.SetBackgroundColour("Green") self.label1 = wx.StaticText(self.panel, -1, 'IP:', style=wx.ALIGN_LEFT) self.label2 = wx.StaticText(self.panel, -1, 'PORT:', style=wx.ALIGN_RIGHT) self.label3 = wx.StaticText(self.panel, -1, 'MSG:', style=wx.ALIGN_LEFT, size=(50, 50)) self.label4 = wx.StaticText(self.panel, -1, 'Send:', style=wx.ALIGN_LEFT, size=(50, 50)) self.textIP = wx.TextCtrl(self.panel, -1) self.textPORT = wx.TextCtrl(self.panel, -1) self.textRecord = wx.TextCtrl(self.panel, -1, style=wx.TE_MULTILINE, size=(520, 200)) self.textSend = wx.TextCtrl(self.panel, -1, style=wx.TE_MULTILINE, size=(520, 30)) self.buttonConnectSvr = wx.Button(self.panel, -1, 'Connect') self.buttonSendMsg = wx.Button(self.panel, -1, 'Send') self.buttonGetFiles = wx.Button(self.panel, -1, 'File') sizer1 = wx.FlexGridSizer(2, 2, 20, 15) sizer1.AddMany( [ (self.label3, 0, wx.ALIGN_LEFT), (self.textRecord, 0, wx.ALIGN_RIGHT), (self.label4, 0, wx.ALIGN_LEFT), (self.textSend, 0, wx.ALIGN_RIGHT), ]) sizer2 = wx.FlexGridSizer(1, 7, 5, 5) sizer2.AddMany( [(self.label1, 0, wx.ALIGN_LEFT), (self.textIP, 0, wx.EXPAND), (self.label2, 0, wx.ALIGN_LEFT), (self.textPORT, 0, wx.ALIGN_RIGHT), (self.buttonConnectSvr, 0, wx.EXPAND), (self.buttonSendMsg, 0, wx.EXPAND), (self.buttonGetFiles, 0, wx.ALIGN_RIGHT) ]) self.border = wx.BoxSizer(wx.VERTICAL) self.border.Add(sizer1, 0, wx.ALL, 15) self.border.Add(sizer2, 0, wx.ALL, 15) self.panel.SetSizerAndFit(self.border) self.Fit() # 窗体大小自适应 if __name__ == '__main__': app=wx.App() frame = ChatWND(None) frame.Show() app.MainLoop()