Python learning Summary 2, python summary 2

Source: Internet
Author: User

Python learning Summary 2, python summary 2

As a cainiao

Recently I learned some usage of wxpython and made some improvements to the last game login code to make it more practical.

After running, the interface is also available. Since I just learned wxpython, the interface is relatively simple, as shown in figure

 

The game logon GUI consists of six parts:

1. Main Program

import windoswindos.windos()

2. Design various buttons and text boxes on the logon GUI. Module name: S,

Import wximport logimport sysdef windos (): # press the logon key and pass the username and password to the log Module def log_in (event): name = ID. getValue () passa = passage. getValue () log. enter (name, passa) def cl (event): sys. exit () # create an application app = wx. app () # create a framework and fix the frame position and size. win = wx. frame (None, title = 'enter your account and password ', style = wx. CAPTION) win. setMaxSize (400,300) win. setMinSize (400,300) win. center () # added background component pan = wx. panel (win) # Add the username input box and its prompt word ID = wx. textCtrl (pan, size = (250,30) ID_name = wx. staticText (pan, label = 'username') # Add the password input box and its prompt word passage = wx. textCtrl (pan, size = (250,30) passage_name = wx. staticText (pan, label = 'Password') # Add the logon button btn = wx. button (pan, label = 'login') clos = wx. button (pan, label = 'logout ') # click the Button event btn. bind (wx. EVT_BUTTON, log_in) clos. bind (wx. EVT_BUTTON, cl) # Set the user name input box and its prompt words to hbox1 = wx at the horizontal position. boxSizer () hbox1.Add (ID_name, proportion = 0, flag = wx. LEFT, border = 30) hbox1.Add (ID, proportion = 0, flag = wx. LEFT, border = 30) # Set the password input box and its prompt words to hbox2 = wx at the horizontal position. boxSizer () hbox2.Add (passage_name, proportion = 0, flag = wx. LEFT, border = 30) hbox2.Add (passage, proportion = 0, flag = wx. LEFT, border = 30) # adjust the logon button at the horizontal position hbox3 = wx. boxSizer () hbox3.Add (btn, proportion = 0, flag = wx. LEFT, border = 120) hbox3.Add (clos, proportion = 0, flag = wx. LEFT, border = 20) # Set the user name input box and password input box and logon button to vbox1 = wx. boxSizer (wx. VERTICAL) vbox1.Add (hbox1, proportion = 0, flag = wx. TOP, border = 20) vbox1.Add (hbox2, proportion = 0, flag = wx. TOP, border = 20) vbox1.Add (hbox3, proportion = 0, flag = wx. TOP, border = 50) pan. setSizer (vbox1) win. show () app. mainLoop ()

  

3. Compare the account, password, and account information to verify that the account and password are correct. The module name is log.

Import wximport pickleimport redisterimport clo_okdef enter (name, password): ''' read the ID file and verify whether the registered ID has ''' while 1: ''' read the ID file, verify whether the account exists and the password is incorrect. '''file2 = open ('account information. pkl ', 'rb') all_ID = [] # Read all user names and passwords of the file, and return while 1: try: a = pickle in the form of a list. load (file2) failed t EOFError: break all_ID.append (a) # convert registration information to dictionary dict_ID = dict (all_ID) # Return ID = dict_ID.keys () if name in ID: if dict_ID [name] = password: wx. exit () clo_ OK .clo (0) break else: wx. exit () clo_ OK .clo (1) else: clo_ OK .clo (2)

  

4. register the program for writing information to the file. The module name is redister.

Import pickleimport clo_okdef redisters (name, password): while 1: '''register id''' ''' read the ID file, verify whether the registered ID exists ''' file2 = open ('account information. pkl ', 'rb') all_ID = [] # Read all user names and passwords of the file, and return while 1: try: a = pickle in the form of a list. load (file2) failed t EOFError: break all_ID.append (a) # convert registration information to dictionary dict_ID = dict (all_ID) # Return ID = dict_ID.keys () # verify whether the registered ID has the if name in ID: clo_ OK .clo (4) break else: ''' to open the ID file, write the registration information ''' file = open ('account information. pkl ',' AB ') account = (name, password) pickle. dump (account, file) file. close () clo_ OK .clo (3) break def text (): redisters () if _ name __= = '_ main _': text ()

  

  

5. GUI registration page program, Module name: win_redister

Import wximport sysimport logimport redisterdef redis (): # press the registration key to pass the user name and password to the log Module def log_in (event): name = ID. getValue () passa = passage. getValue () wx. exit () redister. redisters (name, passa) def cl (event): sys. exit () # create an application app = wx. app () # create a framework and fix the frame position and size. win = wx. frame (None, title = 'register your account and password', style = wx. CAPTION) win. setMaxSize (400,300) win. setMinSize (400,300) win. center () # added background component pan = wx. panel (win) # Add the username input box and its prompt word ID = wx. textCtrl (pan, size = (250,30) ID_name = wx. staticText (pan, label = 'username') # Add the password input box and its prompt word passage = wx. textCtrl (pan, size = (250,30) passage_name = wx. staticText (pan, label = 'Password') # Add the Registration button btn = wx. button (pan, label = 'registration') clos = wx. button (pan, label = 'logout ') # click the Button event btn. bind (wx. EVT_BUTTON, log_in) clos. bind (wx. EVT_BUTTON, cl) # Set the user name input box and its prompt words to hbox1 = wx at the horizontal position. boxSizer () hbox1.Add (ID_name, proportion = 0, flag = wx. LEFT, border = 30) hbox1.Add (ID, proportion = 0, flag = wx. LEFT, border = 30) # Set the password input box and its prompt words to hbox2 = wx at the horizontal position. boxSizer () hbox2.Add (passage_name, proportion = 0, flag = wx. LEFT, border = 30) hbox2.Add (passage, proportion = 0, flag = wx. LEFT, border = 30) # adjust the Registration button at the horizontal position hbox3 = wx. boxSizer () hbox3.Add (btn, proportion = 0, flag = wx. LEFT, border = 120) hbox3.Add (clos, proportion = 0, flag = wx. LEFT, border = 20) # Set the user name input box and password input box and Registration button in the vertical position vbox1 = wx. boxSizer (wx. VERTICAL) vbox1.Add (hbox1, proportion = 0, flag = wx. TOP, border = 20) vbox1.Add (hbox2, proportion = 0, flag = wx. TOP, border = 20) vbox1.Add (hbox3, proportion = 0, flag = wx. TOP, border = 50) pan. setSizer (vbox1) win. show () app. mainLoop ()

  

  

6. Other information displayed, such as logon success, Password error, account error, please register, account registration successful, etc. Module name: clo_ OK

Import wximport logimport export simport win_redisterlis = ['login successfully', 'password error', 'account error please registry', 'account registered successfully ', 'User name already exist'] def clo (k): def close (event): if k = 0: wx. exit () elif k = 1: wx. exit () S. windos () elif k = 2: wx. exit () win_redister.redis () elif k = 3: wx. exit () S. windos () elif k = 4: wx. exit () win_redister.redis () # create an application app = wx. app () # create a framework and fix the frame position and size. win = wx. frame (None, style = wx. CAPTION) win. setMaxSize (400,300) win. setMinSize (400,300) win. center () # added background component pan = wx. panel (win) # Add button btn = wx. button (pan, label = lis [k]) # key event btn. bind (wx. EVT_BUTTON, close) # adjust hbox = wx in the horizontal position of the button. boxSizer () hbox. add (btn, proportion = 1, flag = wx. EXPAND, border = 50) pan. setSizer (hbox) win. show () app. mainLoop ()

  

  

 

  

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.