Easygui python basics. Part 1

This article is about the basics of the easygui module for Python. I recommend using Python 2-x branches, as this will allow tabs and spaces to be combined. Of course, this module is not for writing programs, but as an addition. The article is written for the most beginners, so there will be many comments.

-1. Import easygui

import easygui
from easygui import * # .

0. Hello world!

Let's write Hello, world! on easygui.

msgbox(msg, title, ok_button, image, root) #  (args)

Let's create variables (it is possible without them, but it’s more clear).

msg = "Hello, world!" #
title = "Hello, world!" #
button = "" #
image = "your_image" #    

Now it will be much easier for us to make a message.

msgbox(msg, title, button) #   

Now you will have this window:

image

We wrote Hello, world! Now you can move on.

1. Entering data into an inputbox and writing data to variables

Attention enterbox writes only one character to a variable! Now you can make input.

n = input() # .

Using easygui, you can also make input and write it to a variable. There is an enterbox function for this. Next, create the vvod function and immediately the variables.

def vvod():
    global var1 #    .
    msg = " "
    title = " " #.

Now we need to create a list to create the fields (you can also make several input fields).

Add the following to the vvod function:

fieldValues2 = enterbox(msg, title)
var1 = fieldValues2[0]

Now such a window will appear at startup:

image

Display variable on msgbox screen
msg = "  " + str(var1) # Str + Int = Error
msgbox(msg, "", " !") # 


2. Yes or no?

Easygui has a ynbox function (YesNoBox). This function allows the user to select his further actions. For example, β€œWould you like to continue?” and choosing an option from those proposed. The standard "Yes" and "No", but the functionality of the other buttons you have to do yourself (in the parameters, only these "default_choice" and "cancel_choice"). You can bind them to the keys and make several selections.

Let's set the variables:

msg = "  ?"
title = "!"
choices = ("[<F1>]", "[<F2>]") #   

Well, here is what β€œYes” means, and what β€œNo” means, we will write it ourselves.

ynbox(msg, title, choices, image=None, default_choice="[<F1>]", cancel_choice="[<F2>]") #  "",   "".

The following window will appear:

image

Writing to a variable is similar to inputbox.

3. Authorization. Not SQL!

Now we will make it so that before entering the program you will need to enter a password and login. To do this, we need variables and lists:

def login_procedure():
    while True:
        msg = "   "
        title = ""
        fieldNames = ["", ""] #    ,   ?

The cycle is needed so that if the user is mistaken, he does not have to restart the program. Also, when you click the "Cancel" button, the program crashes. Therefore, we will need a try.

fieldValues = multpasswordbox(msg, title, fieldNames) #  
        try: # 
            USERNAME, PASSWORD = fieldValues[0], fieldValues[1]
        except: #  ,    .
            login_procedure() #    .
            exit(0) #    .

Now we need to set the correct username and password (It is possible in the list and in SQL, but this article talks about the basics of easygui):

# .
        if USERNAME == "login" and PASSWORD == "passtogo":
            msgbox(" !")
            break #  
        else:
            msgbox("   ")

Now we have done everything. A couple of screenshots:

image

image

image

I hope now everything has become clear to you, and the article has benefited you. Thank you for watching! If you have any question, then write about it in the comment.

All Articles