1、首先采用xMind软件画出脑图
从下面的脑图可以看出,需要设计两个界面,一个使登录及注册界面、一个是登录后显示的主界面,还有一个注册成功的弹出对话框。
image.png2、注册及登录界面设计
image.pngwxFormBuilder自动生成的python代码重构后保存为RegisterFrame.py文件如下:
# -*- coding: utf-8 -*-
###########################################################################
## Python code generated with wxFormBuilder (version Jun 17 2015)
## http://www.wxformbuilder.org/
##
## PLEASE DO "NOT" EDIT THIS FILE!
###########################################################################
import wx
import wx.xrc
DEFAULT_REGISTER_LIST = [u"用户名",u"密码",u"确认密码"]
DEFAULT_BUTTON_LIST = [u"登录",u"注册"]
###########################################################################
## Class RegisterFrame
###########################################################################
class RegisterFrame ( wx.Frame ):
def __init__( self, parent, id = -1, UpdateUI=None ):
wx.Frame.__init__ ( self, parent, id = wx.ID_ANY, title = wx.EmptyString, pos = wx.DefaultPosition, size = wx.Size( 500,359 ), style = wx.DEFAULT_FRAME_STYLE|wx.TAB_TRAVERSAL )
#self.UpdateUI = UpdateUI
self.InitUI()
def InitUI(self):
self.SetSizeHints( wx.DefaultSize, wx.DefaultSize )
self.BoxSizer2Init()
self.BoxSizer1Init()
self.SetSizer( self.bSizer1 )
self.Layout()
self.Centre( wx.BOTH )
def staticTextInit(self,name):
m_staticText = wx.StaticText(self, wx.ID_ANY, name, wx.DefaultPosition, wx.DefaultSize, 0 )
m_staticText.Wrap( -1 )
return m_staticText
def textCtrlInit(self):
m_textCtrl = wx.TextCtrl(self,wx.ID_ANY, wx.EmptyString, wx.DefaultPosition, wx.DefaultSize, 0 )
return m_textCtrl
def buttonInit(self,name):
m_button = wx.Button( self, wx.ID_ANY, name, wx.DefaultPosition, wx.DefaultSize, 0 )
m_button.SetBackgroundColour( wx.SystemSettings.GetColour( wx.SYS_COLOUR_HIGHLIGHT ) )
return m_button
def BoxSizer1Init(self):
self.m_staticText1 = self.staticTextInit(DEFAULT_REGISTER_LIST[0])
self.m_staticText2 = self.staticTextInit(DEFAULT_REGISTER_LIST[1])
self.m_staticText3 = self.staticTextInit(DEFAULT_REGISTER_LIST[2])
self.m_textCtrl1 = self.textCtrlInit()
self.m_textCtrl2 = self.textCtrlInit()
self.m_textCtrl3 = self.textCtrlInit()
self.bSizer1 = wx.BoxSizer( wx.VERTICAL )
self.bSizer1.Add( self.m_staticText1, 0, wx.ALL, 5 )
self.bSizer1.Add( self.m_textCtrl1, 0, wx.ALL, 5 )
self.bSizer1.Add( self.m_staticText2, 0, wx.ALL, 5 )
self.bSizer1.Add( self.m_textCtrl2, 0, wx.ALL, 5 )
self.bSizer1.Add( self.m_staticText3, 0, wx.ALL, 5 )
self.bSizer1.Add( self.m_textCtrl3, 0, wx.ALL, 5 )
self.bSizer1.Add(self.bSizer2, 1, wx.EXPAND, 5 )
def BoxSizer2Init(self):
self.bSizer2 = wx.BoxSizer( wx.HORIZONTAL )
self.m_button1 = self.buttonInit(DEFAULT_BUTTON_LIST[0])
self.m_button2 = self.buttonInit(DEFAULT_BUTTON_LIST[1])
self.bSizer2.Add( self.m_button1, 0, wx.ALL, 5 )
self.bSizer2.Add( self.m_button2, 0, wx.ALL, 5 )
def __del__( self ):
pass
3、登录后主界面设计
image.pngwxFormBuilder自动生成的python代码重构后保存为MainFrame.py文件如下:
# -*- coding: utf-8 -*-
###########################################################################
## Python code generated with wxFormBuilder (version Jun 17 2015)
## http://www.wxformbuilder.org/
##
## PLEASE DO "NOT" EDIT THIS FILE!
###########################################################################
import wx
import wx.xrc
MENU_LIST = [u"菜单",u"帮助"]
FIRST_SUBMENU_LIST = [u"试题更新",u"软件更新"]
SECOND_SUBMENU_LIST = [u"关于"]
RADIO_BUTTON_LIST = [u"随机做试题",u"随机做试卷",u"随机做奥数"]
###########################################################################
## Class MainFrame
###########################################################################
###########################################################################
## Class MainFrame
###########################################################################
class MainFrame ( wx.Frame ):
def __init__( self, parent, id = -1, UpdateUI=None ):
wx.Frame.__init__ ( self, parent, id = wx.ID_ANY, title = u"初一数学练习", pos = wx.DefaultPosition, size = wx.Size( 848,600 ), style = wx.DEFAULT_FRAME_STYLE|wx.TAB_TRAVERSAL )
self.UpdateUI = UpdateUI
self.InitUI()
def InitUI(self):
self.SetSizeHints( wx.DefaultSize, wx.DefaultSize )
self.MenuInit()
self.statusBarInit()
self.BoxSizer3Init()
self.SetSizer( self.bSizer3 )
self.Layout()
self.Centre( wx.BOTH )
self.EventBinding()
def EventBinding(self):
# Connect Events
self.Bind( wx.EVT_CLOSE, self.OnClose )
self.m_radioBtn1.Bind( wx.EVT_RADIOBUTTON, self.OnRadioButtonClick )
self.m_radioBtn2.Bind( wx.EVT_RADIOBUTTON, self.OnRadioButtonClick )
self.m_radioBtn3.Bind( wx.EVT_RADIOBUTTON, self.OnRadioButtonClick )
self.m_panel1.Bind( wx.EVT_LEFT_DCLICK, self.OnLeftDClick )
self.m_panel1.Bind( wx.EVT_RIGHT_DCLICK, self.OnRightDClick )
self.m_panel2.Bind( wx.EVT_LEFT_DCLICK, self.OnLeftDClick )
def menuItemInit(self,parent,name):
m_menuItem = wx.MenuItem(parent, wx.ID_ANY, name, wx.EmptyString, wx.ITEM_NORMAL )
return m_menuItem
def MenuInit(self):
self.m_menubar1 = wx.MenuBar( 0 )
self.m_menu1 = wx.Menu()
self.m_menuItem1 = self.menuItemInit(self.m_menu1,FIRST_SUBMENU_LIST[0])
self.m_menu1.Append( self.m_menuItem1 )
self.m_menu1.AppendSeparator()
self.m_menuItem2 = self.menuItemInit(self.m_menu1,FIRST_SUBMENU_LIST[1])
self.m_menu1.Append( self.m_menuItem2 )
self.m_menubar1.Append( self.m_menu1, MENU_LIST[0] )
self.m_menu2 = wx.Menu()
self.m_menuItem3 = self.menuItemInit(self.m_menu2,SECOND_SUBMENU_LIST[0])
self.m_menu2.Append( self.m_menuItem3 )
self.m_menubar1.Append( self.m_menu2, MENU_LIST[0] )
self.SetMenuBar( self.m_menubar1 )
def statusBarInit(self):
self.m_statusBar1 = self.CreateStatusBar( 1, 0, wx.ID_ANY )
def RadioButtonInit(self,name):
radioBtn = wx.RadioButton( self, wx.ID_ANY, name, wx.DefaultPosition, wx.DefaultSize, 0 )
radioBtn.SetValue( True )
radioBtn.SetFont( wx.Font( 8, 70, 90, 90, False, "宋体" ) )
return radioBtn
def _BoxSizer4Init(self):
self.bSizer4 = wx.BoxSizer( wx.VERTICAL )
self.bSizer4.SetMinSize( wx.Size( 50,50 ) )
self.m_radioBtn1 = self.RadioButtonInit(RADIO_BUTTON_LIST[0])
self.bSizer4.Add( self.m_radioBtn1, 0, wx.ALIGN_LEFT|wx.ALL|wx.FIXED_MINSIZE, 5 )
self.m_radioBtn2 = self.RadioButtonInit(RADIO_BUTTON_LIST[1])
self.bSizer4.Add( self.m_radioBtn2, 0, wx.ALIGN_LEFT|wx.ALL|wx.FIXED_MINSIZE, 5 )
self.m_radioBtn3 = self.RadioButtonInit(RADIO_BUTTON_LIST[2])
self.bSizer4.Add( self.m_radioBtn3, 0, wx.ALL, 5 )
def BoxSizer3Init(self):
self._BoxSizer4Init()
self.bSizer3 = wx.BoxSizer( wx.HORIZONTAL )
self.bSizer3.SetMinSize( wx.Size( 800,600 ) )
self.bSizer3.Add( self.bSizer4, 1, wx.ALIGN_TOP|wx.ALL|wx.FIXED_MINSIZE, 5 )
self._BoxSize5Init()
self.bSizer3.Add( self.bSizer5, 1, wx.EXPAND, 5 )
def _BoxSize5Init(self):
self.bSizer5 = wx.BoxSizer( wx.HORIZONTAL )
self.bSizer5.SetMinSize( wx.Size( 750,-1 ) )
self.m_splitter1 = wx.SplitterWindow( self, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.SP_3D|wx.SP_3DBORDER|wx.SP_3DSASH )
self.m_splitter1.Bind( wx.EVT_IDLE, self.m_splitter1OnIdle )
self.m_splitter1.SetMinimumPaneSize( 1 )
self.m_splitter1.SetForegroundColour( wx.SystemSettings.GetColour( wx.SYS_COLOUR_HIGHLIGHT ) )
self.m_splitter1.SetBackgroundColour( wx.SystemSettings.GetColour( wx.SYS_COLOUR_HIGHLIGHTTEXT ) )
self.m_panel1 = wx.Panel( self.m_splitter1, wx.ID_ANY, wx.DefaultPosition, wx.Size( 600,600 ), wx.TAB_TRAVERSAL )
self.m_panel2 = wx.Panel( self.m_splitter1, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.TAB_TRAVERSAL )
self.m_splitter1.SplitVertically( self.m_panel1, self.m_panel2, 0 )
self.bSizer5.Add( self.m_splitter1, 1, wx.EXPAND, 5 )
def __del__( self ):
pass
# Virtual event handlers, overide them in your derived class
def OnClose( self, event ):
event.Skip()
def OnRadioButtonClick( self, event ):
event.Skip()
def OnLeftDClick( self, event ):
event.Skip()
def OnRightDClick( self, event ):
event.Skip()
def m_splitter1OnIdle( self, event ):
self.m_splitter1.SetSashPosition( 0 )
self.m_splitter1.Unbind( wx.EVT_IDLE )
实现效果
两个界面调试文件mainWin.py如下:
import wx
# 导入RegisterFrame.py中内容
import RegisterFrame
import MainFrame
# 创建mainWin类并传入RegisterFrame.RegisterFrame
# 或者MainFrame.MainFrame,用于单独调试两个界面的代码。
#class mainWin(RegisterFrame.RegisterFrame):
class mainWin(MainFrame.MainFrame):
# 实现Button控件的响应函数showMessage
def showMessage(self, event):
self.m_textCtrl1.Clear()
self.m_textCtrl1.SetValue('hello world')
if __name__ == '__main__':
# 下面是使用wxPython的固定用法
app = wx.App()
main_win = mainWin(None)
main_win.Show()
app.MainLoop()
网友评论