About Python to achieve a simple GUI interface display

I feel that some operations on the GUI interface display are more interesting, but the effect achieved by the easygui library in Python is relatively simple, and the operation is also very simple. Here is a small demonstration to have fun...

Download and install easygui

Installing easygui in the python environment is very simple, there are many ways to install, here are two:

  • Direct use pip install easyguiof command to install
  • This link to download the source package, and then install it locally

After the download and installation are complete, you can simply test it. I currently use python environment python2.7.15; test it on VS code (Visual Studio Code):

Simple test
Successful installation! It can be displayed normally!

Import module easygui

In order to use the easygui module, you first need to import it. There are several ways to import:

  • Using import easyguiimport, when calling to be prefixed with easygui, like this:easygui.msgbox(...)
  • Use import easygui as gimport, save this namespace, reducing the amount of code, when you call tog.msgbox(...)
  • Use from easygui import *this easier to call directly using the functionmsgbox(...)

Use easygui

According to the different import methods, the method of using easygui is also a little different. This article uses the second import method (use import easygui as g), and the following examples are based on the second method to demonstrate...

Once imported easygui module is very simple to use, after successfully imported, you can call g.egdemo()to see and try the various functions easygui have, and the result is printed to the console.

easygui functions and functions

Introduction to easygui related functions

Most of the easygui functions have default parameters. The first two parameters of all dialogs are the body of the message and the title of the dialog. Almost all components will display the body of the message and the title of the dialog. The title is an empty string by default. , Which allows us to set as few parameters as possible when using it.

Use button component functions

msgbox()

function msgbox(msg=' ', title=' ', ok_button=' ', image=None, root=None)

默认参数:msgbox (msg=’(Your message goes here)’, title=’ ', ok_button=‘OK’, image=None, root=None)

This function generally calls the first three keywords. If you load an image, assigning a value to image requires downloading other libraries. Examples of functions:

msgbox()

ccbox()

function ccbox(msg=' ', title=' ', choices=(' ', ' '), image=None)

默认参数:ccbox (msg=‘Shall I continue?’, title=’ ', choices=(‘C[o]ntinue’, ‘C[a]ncel’), image=None, default_choice=‘C[o]ntinue’, cancel_choice=‘C[a]ncel’)

Note : "C [o] ntinue" the [o] represents a shortcut key, which means that when a user typed the look oof characters, the equivalent of clicking the "C [o] ntinue" button; 'C [a] ncel 'Similarly.

This function has one more option choices, which provides a choice (only two options can be accommodated!), and returns the boolean value True or False accordingly

ccbox()
In various button components, the default message is "Shall I continue?", so you can call them without any parameters

Here we demonstrate that ccbox() is called without any parameters, and a boolean value is returned when "cancel" is selected or the window is closed:

if g.ccbox(): 
        pass        # 用户选择继续
else: 
        return      # 用户选择取消
inbox ()

function ynbox(msg=' ', title=' ', choices=(' ', ' '), image=None)

默认参数:ynbox (msg=‘Shall I continue?’, title=’ ‘, choices=(’[< F1 >]Yes’, ‘[< F2 >]No’), image=None, default_choice=’[< F1 >]Yes’, cancel_choice=’[< F2 >]No’)

With ccbox () the same, but here the choices the default parameter values is different, [<F1>] said it would on the keyboard F1function keys used as "Yes" shortcut key here is not to do a demo

buttonbox()

function buttonbox(msg=' ', title=' ', choices=('Button1', 'Button2', 'Button3'), image=None, root=None)

默认参数:buttonbox (msg=’’, title=’ ', choices=(‘Button[1]’, ‘Button[2]’, ‘Button[3]’), image=None, images=None, default_choice=None, cancel_choice=None, callback=None, run=True)

Users can use buttonbox() to define their own set of buttons, buttonbox() will display a set of buttons customized by you; when the user clicks any button, the value returned by buttonbox() is different from ccbox(), yes The text content of the button; if the user clicks Cancel or closes the window, it will return to the default option (the first option)

Example:

buttonbox()

indexbox ()

function indexbox(msg=' ', title=' ', choices=(' ', ' '), image=None)

默认参数:indexbox (msg=‘Shall I continue?’, title=’ ', choices=(‘Yes’, ‘No’), image=None, default_choice=‘Yes’, cancel_choice=‘No’)

This function is basically the same as buttonbox(), except that it returns 0 when the user selects the first button, and returns 1 when the second button is selected.

boolbox()

function boolbox(msg=' ', title=' ', choices=(' ', ' '), image=None )

默认参数:boolbox (msg=‘Shall I continue?’, title=’ ‘, choices=(’[Y]es’, ‘[N]o’), image=None, default_choice=‘Yes’, cancel_choice=‘No’)

This function is basically the same as indexbox(), the difference is that it returns True when the user selects the first button, otherwise it returns False

Provide option component functions

choicebox()

function choicebox(msg=' ', title=' ', choices=[ , , ...], preselect=0, callback=None, run=True)

Default parameter: choicebox (msg='Pick an item', title='', choices=[], preselect=0, callback=None, run=True)

choicebox() provides users with a selectable list, using sequence (primitive or list) as options, these options will be sorted alphabetically. Option input is no longer a single element, here is the entire sequence of input, such as a list , Tuples, etc.;

Confirm after selecting the option, the text content of the option content will be returned, otherwise it is none

choicebox()

multchoicebox()

function multchoicebox(msg=' ', title=' ', choices=[ , , ...], preselect=0, callback=None, run=True)

Default parameter: multchoicebox (msg='Pick an item', title='', choices=[], preselect=0, callback=None, run=True)

The function of multchoicebox() is the same as choicebox(), except that it can provide multiple choices, and the return value of multiple choices is a text list of multiple choices:

multchoicebox()

Input message component function

enterbox ()

function enterbox(msg=' ', title=' ', default=' ', strip=True, image=None, root=None)

默认参数:enterbox (msg=‘Enter something.’, title=’ ‘, default=’’, strip=True, image=None, root=None)

enterbox() provides the user with the simplest input box. The return value is the string entered by the user. The default keyword defines the default value of the text box. When the value of strip is True, the leading and trailing spaces of the input will be automatically ignored, False On the contrary

enterbox ()

interbox()

function integerbox(msg='', title=' ', default='', lowerbound=0, upperbound=99, image=None, root=None)

默认参数:integerbox (msg=’’, title=’ ', default=None, lowerbound=0, upperbound=99, image=None, root=None)

integerbox() also provides users with a simple input box. Users can only input integer values ​​within the range (lowerbound parameter sets the minimum value, upperbound parameter sets the maximum value), otherwise the user will be required to re-enter, and the return value is the input value .

interbox()

multenterbox ()

function multenterbox(msg=' ', title=' ', fields=(), values=())

默认参数:multenterbox (msg=‘Fill in values for the fields.’, title=’ ', fields=[], values=[], callback=None, run=True)

Among them, values ​​is the default value entered, and feilds is the name of the entry that needs to be filled in, all filled in with a list; the return value is a list of all filled values

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

  • If the user enters less than the options, the value in the return list is filled with the options that the user did not enter with an empty string
  • If the user enters more values ​​than options, the value in the returned list will be truncated to the number of options
  • If the user cancels the operation, return the value of the list in the domain or the None value

Examples are as follows:

multenterbox ()

Input password component function

In GUI programming, sometimes the interface needs to enter sensitive information such as passwords, so that the interface looks like this: **************

Some basic functions are provided in easygui for users to achieve this.

passwordbox ()

function passwordbox(msg=' ', title=' ', default=' ', image=None, root=None)

默认参数:passwordbox (msg=‘Enter your password.’, title=’ ‘, default=’’, image=None, root=None)

The passwordbox() function can do this. It has the same style as enterbox(). The difference is that the content entered by the user is displayed with an asterisk (*). The function returns the string entered by the user. Example:

passwordbox () - 1
The following example shows the actual string entered:

passwordbox () - 2

multpasswordbox()

function multpasswordbox(msg=' ', title=' ', fields=(), values=(), callback=None, run=True)

默认参数:multpasswordbox (msg=‘Fill in values for the fields.’, title=’ ', fields=(), values=(), callback=None, run=True)

multpasswordbox() uses the same interface as multenterbox(), but is a more practical type. Only the last box is anonymous, that is, the password input box; everything else is the same, and the return value is in list form

multpasswordbox()

Display text component function

textbox()

function textbox(msg='', title=' ', text='', codebox=False, callback=None, run=True)

The textbox() function will display text content (auto-wrap) in proportional font (parameter codebox=True is set to monospace font) by default. This function is suitable for displaying general written text.

Note : The text parameter sets the content of the editable text area, which can be a string, a list, or an ancestor type.

textbox()

codebox()

function codebox(msg='', title=' ', text='')

codebox() displays the text content in a monospace font (without automatic line wrapping), which is equivalent to textbox(codebox=True)

Directory and file functions

A common scenario in GUI programming is to require the user to enter the directory and file name. Easygui provides some basic functions for the user to browse the file system and select a directory or file.

diropenbox ()

function diropenbox(msg=None, title=None, default=None)

The diropenbox() function is used to provide a dialog box that returns the directory name selected by the user (with the full path), or None if the user selects "Cancel".

The default parameter is used to set the default open directory (please make sure that the set directory already exists).

diropenbox ()

fileopenbox()

function fileopenbox(msg=None, title=None, default='*', filetypes=None, multiple=False)

The fileopenbox() function is used to provide a dialog box to return the file name selected by the user (with the full path), or None if the user selects "Cancel".

About the setting method of 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.
  • default The default parameter is'*', which matches all formats of files.

E.g:

default="c:/fishc/.py " shows all the Python files in the C:\fishc folder.
default="c:/fishc/test.py
" means that all Python files in the C:\fishc folder whose names start with test are displayed.

About the setting method of filetypes parameter:

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

The last is the multiple parameter. If it is True, it means that multiple files can be selected at the same time.

fileopenbox()

filesavebox()

function filesavebox(msg=None, title=None, default='', filetypes=None)

The filesavebox() function provides a dialog box for selecting the path (with the full path) where the file needs to be saved. If the user selects "Cancel", it returns None.

The default parameter should contain a file name (for example, the name of the file that needs to be saved currently), of course, it can also be set to empty, or include a wildcard for the file format mask.

For the setting method of the filetypes parameter, please refer to the fileopenbox() function.

filesavebox()

Exception catch function

exceptionbox()

Use easygui to write GUI programs, sometimes it is inevitable that exceptions will occur. When your application crashes, the stack trace may be thrown out or written to the stdout standard output function.

easygui provides a better way to handle exceptions through the exceptionbox() function.

When an exception occurs, exceptionbox() will display the stack trace in a codebox() and allow you to do further processing.

exceptionbox() is easy to use. Here is an example:

try:
    print('Hello Word !!!')
    int('Hello Word') # 这里会产生异常
except:
    g.exceptionbox()

exceptionbox()

Well, the above is just a simple use of the easygui display interface, its function is very simple, briefly explain some of the functions, and also record it here...

Guess you like

Origin blog.csdn.net/My_CSDN_IT/article/details/113863622