[Python extension Read EasyGui learning document [more detailed Chinese Version ]],

Source: Internet
Author: User

[Go to EasyGui learning document for Python extension ]],

Translated from official documents: http://easygui.sourceforge.net/tutorial/index.html

Translation Editor: xiaojiyu. You are welcome to reprint this article. Please ensure the integrity of the original article!

Demonstrate how to use Python 3.3.3


0. Install EasyGui

Official Website: http://easygui.sourceforge.net

Latest Version: easygui-0.96.zip

Upload click File Name Download Attachment



Install using the standard method:

  • Use the command window to switch to the easygui-docs-0.96 directory
  • In Windows, run C: \ Python33 \ python.exe setup. py install.
  • [In Linux or Mac] sudo/usr/bin/python33 setup. py install



1. We recommend that you do not run EasyGui on IDLE.

EasyGui runs on Tkinter and has its own event loop, while IDLE is also an application written by Tkinter and has its own event loop. Therefore, when both of them run simultaneously, there may be conflicts and unpredictable results. Therefore, if you find that your EasyGui program has such a problem, try to run your program outside IDLE.


2. A simple example

In EasyGui, all GUI interactions are called through simple functions. The following simple example shows that EasyGui is really Easy!



3. demo of EasyGui Functions


To run the demo program of EasyGui, call EasyGui in the command line as follows:


Or you can call it from IDE (such as IDLE, PythonWin, Wing, and so on:


After a successful call, you can try various functions of EasyGui and print the result you selected to the console.

1. png (15.58 KB, downloads: 3)

Download the attachment and save it to the album.

Upload

 




4. Import EasyGui

To use the EasyGui module, you should first import it. The simplest Import Statement is:


If you use the preceding method to import data, you must add the prefix EasyGui before the function when using the easygui function, as shown in the following figure:


Another option is to import the entire EasyGui package:


This makes it easier for us to call EasyGui functions. You can write code like this:


The third solution is to use an import statement similar to the following:


This allows you to maintain the namespace of EasyGui and reduce the number of typing. After the import, you can call the EasyGui function as follows:



5. Use EasyGui


Once your module imports EasyGui, the GUI operation is a simple problem of calling several parameters of the EasyGui function.

For example, you can use EasyGui to implement the famous "Hello, world !" The program is as follows:

2. png (1.53 KB, downloads: 2)

Download the attachment and save it to the album.

Upload

 




6. default parameters of EasyGui Functions

For all functions, the first two parameters are the message and title. According to this rule, in some cases, this may not be the most favorable arrangement for users (for example, ignore message parameters when obtaining directories and file names in the dialog box ), however, I think it is more important to keep this consistency throughout all the windows!

Most EasyGui functions have default parameters, and almost all components display a message and title. The title is a null string by default, and the information usually has a simple default value.

This allows you to set as few parameters as possible. For example, parameters in the header of the msgbox () function are optional. Therefore, you can specify only one message parameter when calling msgbox, for example:


You can also specify the title parameter and Message Parameter, for example:

 



In various button components, the default message is "Shall I continue? ", So you can call them without any parameters. Here we demonstrate to call ccbox () without any parameters. When "cancel" is selected or the window is closed, a Boolean value is returned:



7. Use the keyword parameter to call the EasyGui Function


You can also use the keyword parameter to call the EasyGui function. (For example, if you forget your shoes, go to "getting started with Python", and talk about how to fill your mind in 18th)

Now, if you need to use a button component, but you do not want to specify the title parameter (the second parameter), you can still use the keyword Parameter Method to specify the choices parameter (the third parameter ), like this:

 

4. png (5.57 KB, downloads: 2)

Download the attachment and save it to the album.

Upload

 




8. Use button Components

EasyGui creates a series of functions on buttonbox () for calling as needed.


8.1 msgbox ()

Msgbox (msg = '(Your message goes here)', title = '', OK _button = 'OK', image = None, root = None)

Msgbox () displays a message and provides an "OK" button. You can specify any message and title, and you can even rewrite the content of the "OK" button.
The following is an example function of msgbox:


The simplest way to override the "OK" button is to use the keyword parameter:

5. png (2.14 KB, downloads: 2)

Download the attachment and save it to the album.

Upload

 




8.2 ccbox ()

Ccbox (msg = 'Shall I continue? ', Title = '', choices = ('contine', 'cancel'), image = None)

Ccbox () provides a choice: Continue or Cancel, and returns 1 (select Continue) or 0 (select Cancel ).

Note that ccbox () is 1 or 0 of the integer type, not True or False of the boolean type. But you can still write it like this:



8.3 ynbox ()


Ynbox (msg = 'Shall I continue? ', Title = '', choices = ('yes', 'no'), image = None)

I don't even know what the author designed these two things ......


8.4 buttonbox ()

Buttonbox (msg = '', title ='', choices = ('button1', 'button2', 'button3'), image = None, root = None)

You can use buttonbox () to define a set of buttons. buttonbox () will display a set of buttons you have defined.

When you click any button, buttonbox () returns the text of the button. If you cancel or close the window, the default option (the first option) is returned ). See the example below:

6. png (2.92 KB, downloads: 2)

Download the attachment and save it to the album.

Upload

 




8.5 indexbox ()

Indexbox (msg = 'Shall I continue? ', Title = '', choices = ('yes', 'no'), image = None)

Basically the same as above, the difference is that when the user selects the first button, return the serial number 0, and when the second button is selected, return the serial number 1.


8.6 boolbox ()

Boolbox (msg = 'Shall I continue? ', Title = '', choices = ('yes', 'no'), image = None)

If the first button is selected, 1 is returned; otherwise, 0 is returned.


9. How to display images in the buttonbox

When you call a buttonbox function (such as msgbox (), ynbox (), indexbox (), and so on), you can assign values to the keyword parameter image. gif images (note that only GIF images are supported ):




10. provide users with a series of options

10.1 choicebox ()

Choicebox (msg = 'pick something. ', title = '', choices = ())

The button component provides you with a simple button option. However, if you have many options or the option content is particularly long, a better policy is to provide them with an optional list.

Choicebox () provides you with an optional list that uses sequence (ancestor or list) as an option. These options are sorted in a case-insensitive manner before being displayed.

You can also use the keyboard to select one of the options (tangled, but not important at all ):

  • For example, when you press the "g" key on the keyboard, the first option starting with "g" will be selected. Press the "g" key again to select the next option starting with "g. When selecting the last option starting with "g", pressing the "g" key again will return to the first option starting with "g" at the beginning of the list.
  • If the option does not start with "g", the option that starts with the character sorted before "g" ("f") is selected.
  • If no characters are sorted before "g", the first element in the list will be selected.

8. png (11.12 KB, downloads: 2)

Download the attachment and save it to the album.

Upload

 




10.2 multchoicebox ()

Multchoicebox (msg = 'pick as your items as you like. ', title = '', choices = (), ** kwargs)

The multchoicebox () function also provides an optional list. Unlike choicebox (), multchoicebox () supports selecting 0, 1, or multiple options at the same time.

The multchoicebox () function also uses a sequence (ancestor or list) as an option. These options are sorted by case-insensitive method before being displayed.

9. png (16.85 KB, downloads: 2)

Download the attachment and save it to the album.

Upload

 




11. Ask the user to enter the message

11.1 enterbox ()

Enterbox (msg = 'enter something. ', title = '', default ='', strip = True, image = None, root = None)

Enterbox () provides users with the simplest input box. The return value is the string entered by the user. By default, the returned values will automatically remove spaces at the beginning and end. If you need to retain spaces at the beginning and end, set the strip parameter to False.

10. png (4.65 KB, downloads: 2)

Download the attachment and save it to the album.

Upload

 




11.2 integerbox ()

Integerbox (msg = '', title ='', default = '', lowerbound = 0, upperbound = 99, image = None, root = None, ** invalidKeywordArguments)

Integerbox () provides you with a simple input box. You can only enter an integer value in the range (the lowerbound parameter sets the minimum value and the upperbound parameter sets the maximum value). Otherwise, you are required to re-enter the value.


11.3 multenterbox ()

Multenterbox (msg = 'fill in values for the fields. ', title = '', fields = (), values = ())

Multenterbox () provides users with multiple simple input boxes. Pay attention to the following points:

  • If the value entered by the user is less than the option, the return value in the list is filled with an empty string with the option entered by the user.
  • If you enter more values than the options, the values in the returned list are truncated to the number of options.
  • If you cancel the operation, the list value or None value in the domain is returned.

11. png (9.34 KB, downloads: 2)

Download the attachment and save it to the album.

Upload

 




12. Ask the user to enter the password

Sometimes we need to ask the user to enter the password, that is, what the user inputs looks like "*******".


12.1 passwordbox ()

Passwordbox (msg = 'enter your password. ', title = '', default ='', image = None, root = None)

The style of passwordbox () is the same as that of enterbox (). The difference is that the content entered by the user is displayed with "*" and the string entered by the user is returned:




12.2 multpasswordbox ()

Multpasswordbox (msg = 'fill in values for the fields. ', title = '', fields = (), values = ())

Multpasswordbox () and multenterbox () use the same interface, but when it is displayed, the last input box is displayed as a password ("*"):

13. png (4.67 KB, downloads: 2)

Download the attachment and save it to the album.

Upload

 




13. display text

EasyGui also provides functions for displaying text.


13.1 textbox ()

Textbox (msg = '', title ='', text = '', codebox = 0)

By default, the testbox () function displays text content in a proportional font (the codebox = 1 parameter is set to an equal-width font). This function is suitable for displaying general written text.

Note: The text parameter (the third parameter) can be of the string type, list type, or ancestor type.

14. png (36.23 KB, downloads: 2)

Download the attachment and save it to the album.

Upload

 




13.2 codebox ()

Codebox (msg = '', title ='', text = '')

Codebox () displays text content in an equal-width font, equivalent to textbox (codebox = 1)

Note: The font of the same width is ugly. If you don't believe it, try @_@


14. directories and files

A common scenario in GUI programming is to require users to input directories and file names. EasyGui provides some basic functions for users to browse the file system and select a directory or file.


14.1 diropenbox ()

Diropenbox (msg = None, title = None, default = None)

The diropenbox () function is used to provide a dialog box that returns the name of the selected directory (with the complete path). If you select "Cancel", None is returned.

The default parameter is used to set the default Open Directory (ensure that the directory already exists ).

15. png (15.79 KB, downloads: 2)

Download the attachment and save it to the album.

Upload

 




14.2 fileopenbox ()

Fileopenbox (msg = None, title = None, default = '*', filetypes = None)

The fileopenbox () function is used to provide a dialog box that returns the selected file name (with the complete path). If you select "Cancel", None is returned.

How to set the default parameter:

  • The default parameter specifies a default path, which usually contains one or more wildcards.
  • If the default parameter is set, fileopenbox () displays the default file path and format.
  • The default parameter is '*' by default, that is, files in all formats are matched.


For example:

  • Default = "c:/fishc/*. py": displays all Python files in the C: \ fishc folder.
  • Default = "c:/fishc/test *. py": displays all Python files whose names start with test in the C: \ fishc folder.


The filetypes parameter setting method is as follows:

  • It can be a list of strings containing the file mask, for example: filetypes = ["*. txt"]
  • It can be a string list. The last string in the list is a description of the file type, for example, filetypes = ["*. css ",["*. htm ","*. html "," HTML files "]






14.3 filesavebox ()

Filesavebox (msg = None, title = None, default = '', filetypes = None)

The filesavebox () function provides a dialog box for you to select the path to save the file (with the complete path). If you select "Cancel", None is returned.

The default parameter should contain a file name (for example, the file name to be saved). Of course, you can also set it to null or include a wildcard with a file format mask.

For more information about filetypes parameter settings, see the preceding section.

17. png (45.43 KB, downloads: 2)

Download the attachment and save it to the album.

Upload

 




15. Remember user settings

15.1 EgStore

A common scenario in GUI programming is to require the user to set parameters and save them so that the user can remember their settings the next time they use your program.

To store and restore user settings, EasyGui provides a class called EgStore. To remember some settings, your application must define a class (temporarily referred to as "setting" class, although you can set it with the name you want at Will) inherited from the EgStore class.

Then, your application must create an object of this class (temporarily called "setting" object ).

Set the class Constructor (_ init _ method) to initialize all the values you want to remember.

Once you do this, you can instantiate the variable in the "Settings" object by setting the value, so that you can easily remember the settings. Then, use the settings. store () method to persistently set objects on the hard disk.

The following is an example of creating a "set" class:


The following is an example of setting an object:



16. Capture exceptions


Predictionbox ()

Sometimes exceptions may occur when you use EasyGui to write GUI programs. Of course, this depends on how you run your application. When your application crashes, stack tracing may be thrown or written to stdout standard output functions.

EasyGui uses the exceptionbox () function to provide a better way to handle exceptions. When an exception occurs, exceptionbox () displays the stack trace in a codebox () and allow further processing.

Predictionbox () is easy to use. The following is an example:

18. png (11.87 KB, downloads: 3)

Download the attachment and save it to the album.

Upload

 

 




Related Article

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.