English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Detailed Introduction to Python Module EasyGui

Detailed Introduction to Python Module EasyGui

Preface:

As I want to develop some simple interfaces with Python on Windows, I found the EasyGui library that is easy to use. Let's share a simple usage below.

Reference Links:Official Website Tutorial

Next, I will demonstrate how to use this module from simple to slightly complex. I hope it can be a little help for those who are just getting started with easygui :)-)

msgBox,ccbox,ynbox

# coding:utf-8
#  __author__ = 'Mark sinoberg'
#  __date__ = '"2016/5/25"
#  __Desc__ = A simple MessageBox-like window similar to Java
import easygui
title = easygui.msgbox(msg='Prompt Information',title='Title Part',ok_button="OK")
msg = easygui.msgbox('Hello Easy GUI')
print 'Return value: ' + msg
ccbox = easygui.ccbox("here is Continue | Cancel Box!")
print 'Return value: ' + str(ccbox)
ynbox = easygui.ynbox("Yes Or No Button Box!")
print 'Return value: ' + str(ynbox)

bottonbox

# coding:utf-8
#  __author__ = 'Mark sinoberg'
#  __date__ = '"2016/5/25"
#  __Desc__ = Allows you to choose a simple interface at first, the second parameter is a list
import easygui
# choice = easygui.buttonbox("This is the prompt message:\n", title='Choose One', choices=['one' \
#   , 'two', 'three'])
# easygui.msgbox('You have chosen: ') + str(choice))
#
# # choices must have only two parameters, the one chosen will be returned1, otherwise return 0
# bool = easygui.boolbox('msg prompt information', title='Title Part', choices=['A', 'B'])
# easygui.msgbox(bool)
image = 'me.jpg'
msg = 'Here is my photo, a python fan also'
choices = ['Yes','No','Not Sure']
title = 'Am I handsome?'
easygui.buttonbox(msg,title,image=image,choices=choices)

choicebox

# coding:utf-8
#  __author__ = 'Mark sinoberg'
#  __date__ = '"2016/5/25"
#  __Desc__ = Select one from a list, and a return value will appear
import easygui
msg = 'Choose one from this list of items you like'
title = 'You must choose one'
choices = ['1','2','3','4','5','6','7]
answer = easygui.choicebox(msg,title,choices)
print 'You selected:' + str(answer)

enterbox

# coding:utf-8
#  __author__ = 'Mark sinoberg'
#  __date__ = '"2016/5/25"
#  __Desc__ = Controls that can meet user input requirements
import easygui
st = easygui.enterbox("Please enter a paragraph of text:\n")
print "You entered: " + str(st)

mutilchoicebox

# coding:utf-8
#  __author__ = 'Mark sinoberg'
#  __date__ = '"2016/5/25"
#  __Desc__ = A multi-select list item. Hahaha, this version seems to have a problem. My multi-select is not actually implemented
import easygui
msg = 'Choose one from this list of items you like'
title = 'You must choose one'
choices = (1,2,3,4,5,6,7,8,9)
answer1 = easygui.multchoicebox(msg,title,choices)
for item in answer1:
  print item

intenterbox,passenterbox

# coding:utf-8
#  __author__ = 'Mark sinoberg'
#  __date__ = '"2016/5/25"
#  __Desc__ = Provides a simple input box for users, which can only be a given numeric range
import easygui
msg = 'Please enter a number, ranging from 0'-100'
title = 'Limited to numeric type'
lowerbound = 0
upperbound = 100
default = ''
image = 'me.jpg'
result = easygui.integerbox(msg,title,default,lowerbound,upperbound,image)
print result

textbox,codebox

# coding:utf-8
#  __author__ = 'Mark sinoberg'
#  __date__ = '"2016/5/25"
#  __Desc__ = easygui also provides support for a large amount of text, as well as support for code text
import easygui
msg = 'Support for large text'
title = 'Text Code'
text = 'abcdefghijklmnopqrstuvwxyzABCDEFGHJIKLMNOPQRSTUVWXYZ0123456789-/"
textContent = easygui.textbox(msg,title,text)
codeContent = easygui.codebox(msg,title,)
print textContent
print codeContent
# D:\Software\Python2\python.exe E:/Code/Python/MyTestSet/easygui_/text_codebox.py
# abcdefghijklmnopqrstuvwxyzABCDEFGHJIKLMNOPQRSTUVWXYZ0123456789-/
# public class HelloWorld{
#  public static void main(String []args) {
#    System.out.println("Hello World!");
# }
# }
#
# Process finished with exit code 0

diropenbox

# coding:utf-8
#  __author__ = 'Mark sinoberg'
#  __date__ = '"2016/5/25"
#  __Desc__ = This function is used to provide a dialog that returns the full path of the directory selected by the user
# If Cancel is selected, the default return value is None
import easygui
msg = 'Select a file, and the complete directory of the file will be returned'
title = 'File Selection Dialog'
default = r'F:\flappy-bird'
full_file_path = easygui.diropenbox(msg, title, default)
print 'The complete path of the selected file is:' + str(full_file_path)
# D:\Software\Python2\python.exe E:/Code/Python/MyTestSet/easygui_/diropenbox.py
# The complete path of the selected file is: F:\flappy-bird
#
# Process finished with exit code 0

fileopenbox

# coding:utf-8
#  __author__ = 'Mark sinoberg'
#  __date__ = '"2016/5/25"
#  __Desc__ = This method is used to provide a dialog that returns the full path of the file selected by the user, and returns None if Cancel is selected
# The default value is "c:",/fishc/*.py" will display all Python files in the C:\fishc folder.
# The default value is "c:",/fishc/test*.py" will display all Python files starting with 'test' in the C:\fishc folder.
# The 'filetypes' parameter is a list of strings containing file masks, remember it's a list. For example: filetypes = ["*.css", ["*.htm", "*.html", "HTML files"]]
import easygui
msg = 'Return the complete path of the selected file, selecting Cancel returns None'
title = 'File Selector'
default = 'E:'/Code/Python/MyTestSet/easygui/*.py'
opened_files = easygui.fileopenbox(msg,title,default,multiple=True)
for item in opened_files:
  print item
# D:\Software\Python2\python.exe E:/Code/Python/MyTestSet/easygui_/fileopenbox.py
# E:\Code\Python\MyTestSet\easygui_\me.jpg
# E:\Code\Python\MyTestSet\easygui_\buttonbox.py
# E:\Code\Python\MyTestSet\easygui_\choicesbox.py
# E:\Code\Python\MyTestSet\easygui_\diropenbox.py
# E:\Code\Python\MyTestSet\easygui_\enterbox.py
# E:\Code\Python\MyTestSet\easygui_\fileopenbox.py
# E:\Code\Python\MyTestSet\easygui_\integerbox.py
#
# Process finished with exit code 0

filesavebox

# coding:utf-8
#  __author__ = 'Mark sinoberg'
#  __date__ = '"2016/5/25"
#  __Desc__ = This function provides a dialog box for the user to select the path (with the complete path) where the file needs to be saved. Selecting Cancel returns None
#        The default parameter should contain a filename (for example, the current file to be saved), of course, you can also set it to empty, or contain a wildcard file format mask.
#        filetypes refer to the above fileopenbox
import easygui
msg = 'Save your file'
title = "to Save File"
default = 'E:'/Code/Python/MyTestSet/easygui/newFile.*"
savedfile = easygui.filesavebox(msg,title,default)
print savedfile
print 'Of course, this is just a complete path with a filename, it will not really save as a file. To save a file, you need to use other libraries'
# D:\Software\Python2\python.exe E:/Code/Python/MyTestSet/easygui_/filesavebox.py
# E:\Code\Python\MyTestSet\easygui_\newFile.doc
# Of course, this is just a complete path plus a filename, and it will not really save it as a file. To save a file, you need to use other libraries
#
# Process finished with exit code 0

exceptionbox

# coding:utf-8
#  __author__ = 'Mark sinoberg'
#  __date__ = '"2016/5/25"
#  __Desc__ = This is a very useful dialog box. When an application encounters an exception, it can provide a friendly interface prompt to the user
import easygui
try:
  int('Exception')
except:
  easygui.exceptionbox('int type data conversion error! Please check your data type!')
# A window will pop up, and the content information can be defined by yourself, as above. The following content is the error information traced.
# Traceback (most recent call last):
#  File "E:/Code/Python/MyTestSet/easygui_/exceptionbox.py", line 10, in <module>
#   int('Exception')
# ValueError: invalid literal for int() with base 10: 'Exception'

Summary

After looking at these examples, you must be very confident in developing simple desktop applications with easygui. (^__^) Hahaha...

But, for more complex tasks, just mastering these basics is far from enough. So we need to explore other related Python modules. This way, in actual development, we can choose the most suitable module for development according to the difficulty of the task.

Thank you for reading, I hope it can help everyone. Thank you for your support of this site!