1、首先采用xMind軟件畫出腦圖
從下面的腦圖可以看出馋贤,需要設計兩個界面赞别,一個使登錄及注冊界面、一個是登錄后顯示的主界面配乓,還有一個注冊成功的彈出對話框仿滔。
2、注冊及登錄界面設計
wxFormBuilder自動生成的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犹芹、登錄后主界面設計
wxFormBuilder自動生成的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"隨機做奧數(shù)"]
###########################################################################
## 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"初一數(shù)學練習", 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 )
實現(xiàn)效果
兩個界面調(diào)試文件mainWin.py如下:
import wx
# 導入RegisterFrame.py中內(nèi)容
import RegisterFrame
import MainFrame
# 創(chuàng)建mainWin類并傳入RegisterFrame.RegisterFrame
# 或者MainFrame.MainFrame崎页,用于單獨調(diào)試兩個界面的代碼。
#class mainWin(RegisterFrame.RegisterFrame):
class mainWin(MainFrame.MainFrame):
# 實現(xiàn)Button控件的響應函數(shù)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()