[ TOC]
Introduction to various GUIs under Python and comparison of their advantages and disadvantages:
GUI Programming | Introduction Features | Pros and Cons |
---|---|---|
PyQt | Python implements 440 classes and 6000 functions or methods in the packaging of the cross-platform GUI tool set Qt. PyQt is implemented as a Python plugin. | A more popular alternative to Tkinter, with very powerful functions. You can use Qt to develop beautiful interfaces, and you can use PyQt to develop beautiful interfaces. Cross-platform support is good, but there seem to be some problems with commercial licensing. |
Tkinter | is bound to the Tk GUI tool set of Python, which is the Tcl code packaged by Python. It is implemented by the Tcl interpreter embedded in the Python interpreter. The calls of Tkinter are converted into Tcl commands and then handed over to the Tcl interpreter for interpretation. Implement the Python GUI interface. Comparing Tk and other language bindings such as PerlTk is directly implemented by the C library in Tk. | The longest history of Python is the de facto standard GUI. The standard interface of the Tk GUI tool set in Python has been included in the standard Python Windows installation. The famous IDLE is to use Tkinter to realize the creation of the GUI. The GUI is simple, learn and use It's also simple. |
wxPython | Python is a wrapper of the cross-platform GUI tool set wxWidgets (written in C++), implemented as an extension module of Python. | A popular alternative to Tkinter, which performs well on various platforms. |
PyGTK | A series of Python wrappers for GTK+ GUI library. | A more popular alternative to Tkinter. Many of the well-known applications under Gnome use PyGTK to implement the GUI. For example, BitTorrent, GIMP and Gedit all have optional implementations. It seems that it does not perform well on the Windows platform. There is nothing wrong with it, after all, the GUI library of GTK is used. |
PySide | Another Python wrapper for the cross-platform GUI tool set Qt, bundled in Python, was originally implemented by the BoostC++ library and later migrated to Shiboken. | A popular alternative to Tkinter is similar to the above. As for the difference between the two |
A very simple GUI programming module in easygui | Python. Unlike other GUI generators, it is not event-driven | implemented by calling functions, and it is simple to use; it provides users with a simple GUI interactive interface without the programmer knowing anything. Any details about tkinter, frames, widgets, callbacks or lambdas. |
(1) easygui module
(2) wxPython module
**1.1 What is EasyGUI? **
EasyGUI is a very simple GUI programming module in Python. Unlike other GUI generators, it is not event-driven.
On the contrary, all GUI interaction can be realized through simple function calls. EasyGUI provides users with a simple GUI interaction interface, without the programmer knowing any details about tkinter, framework, components, callbacks or lambda.
EasyGUI runs on Tkinter and has its own event loop, and IDLE is an application written by Tkinter and also has its own event loop. Therefore, when the two are running at the same time, there may be conflicts and unpredictable results. So if you find that your EasyGUI program has such a problem, please try to run your program outside of IDLE.
Environment version: Python 3.7.2, easygui-0.98.1-py3.7.egg
Official website: https://github.com/robertlugg/easygui
Use pip to install: pip install easygui
1.2 Import EasyGUI module and method introduction
In order to use EasyGUI this module, you should import it first,
The import statement is:
import easygui
from easygui import*import easygui as g
# This is how EasyGUI is called on the command line, and it can also be used from an IDE (such as IDLE, PythonWin, Wing,Etc.) on call:
Python3/Lib/site-packages/easygui-0.98.1-py3.7.egg/easygui/easygui.py
easygui.egdemo()
List of module methods:
(0) easygui.egdemo() #EasyGUI demo program
''' Message dialog'''msgbox(msg='(Your message goes here)', title=' ', ok_button='OK', image=None, root=None) #Message Box
enterbox(msg='Enter something.', title=' ',default='', strip=True, image=None, root=None) #Input box,The return value is the string entered by the user;integerbox(msg='', title=' ',default=None, lowerbound=0, upperbound=99, image=None, root=None) #数值Input box
# Multiple simple input boxes,If the value entered by the user is less than the option, the value in the returned list will be filled with an empty string and the user will be truncated if there are too many options entered.,The user cancels the operation and returns None
multenterbox(msg='Fill in values for the fields.', title=' ', fields=[], values=[], callback=None, run=True)passwordbox(msg='Enter your password.', title=' ',default='', image=None, root=None) #Password input box(To*Number form display)'''Button dialog'''
# Display a message and provide an "OK" button, you can specify any message and title, override the OK button
# When selecting "cancel" or closing the window, it returns a boolean value of 0, otherwise it is 1
# Note: "C[o]ntinue"[o]Represents a shortcut key, which means that when the user taps the o character on the keyboard, it is equivalent to clicking "C[o]ntinue" button.
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')
# With ccbox()The same, except that the default choices parameter value is different here.[<F1>]Means to use the F1 function key on the keyboard as a shortcut key of "Yes".
ynbox(msg='Shall I continue?', title=' ', choices=('[<F1>]Yes','[<F2>]No'), image=None, default_choice='[<F1>]Yes', cancel_choice='[<F2>]No')
# Use buttonbox()Define your own set of buttons, buttonbox()A set of buttons customized by you will be displayed.
# When the user clicks any button, the buttonbox()Returns 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)
buttonbox(msg='', title=' ', choices=('Button[1]','Button[2]','Button[3]'), image=None, images=None, default_choice=None, cancel_choice=None, callback=None, run=True)
# Basically with buttonbox()The same, the difference is that when the user selects the first button, it returns the number 0, and when the second button is selected, it returns the number 1 indexbox(msg='Shall I continue?', title=' ', choices=('Yes','No'), image=None, default_choice='Yes', cancel_choice='No')boolbox(msg='Shall I continue?', title=' ', choices=('[Y]es','[N]o'), image=None, default_choice='Yes', cancel_choice='No') #It returns True if the first button is selected, otherwise it returns False.
''' select/Checkbox'''
# The button component is convenient to provide users with a simple button option, but if there are many options, or the content of the options is particularly long, a better strategy is to provide them with a selectable list
choicebox(msg='Pick an item', title='', choices=[], preselect=0, callback=None, run=True) #Single box
multchoicebox(msg='Pick an item', title='', choices=[], preselect=0, callback=None, run=True) #Allow users to select 0, 1 or multiple options at the same time
''' Show text box'''
# Note: The text parameter sets the content of the editable text area, which can be a string, a list or an ancestor type.
textbox(msg='', title=' ', text='', codebox=False, callback=None, run=True)codebox(msg='', title=' ', text='') #Display text content in monospace font (without automatic line wrapping), equivalent to textbox(codebox=True)'''File directory selection box'''
# The function is used to provide a dialog box to return the directory name selected by the user (with the full path), if the user selects "Cancel", it returns None
diropenbox(msg=None, title=None,default=None)fileopenbox(msg=None, title=None,default='*', filetypes=None, multiple=False)filesavebox(msg=None, title=None,default='', filetypes=None) #The function provides a dialog box for selecting the path where the file needs to be saved (with the full path)
# About the setting method of the default parameter:
default parameter specifies a default path, usually contains one or more wildcards.
If the default parameter is set, fileopenbox()Show the default file path and format.
default The default parameter is'*', Which matches all formats of files.
# E.g:
default="c:/fishc/*.py"Which shows C:\All Python files in the fishc folder.
default="c:/fishc/test*.py"Which shows C:\All Python files in the fishc folder whose names start with test.
# About the setting method of filetypes parameter:
Can be a list of strings containing file masks, for example: filetypes=["*.txt"]
It can be a list of strings, the last string of the list is the description of the file type, for example: filetypes=["*.css",["*.htm","*.html","HTML files"]]
1.3 EgStore stored procedure
A common scenario in GUI programming is to ask the user to set the parameters, and then save them so that the user can remember his settings the next time they use your program; in order to realize the process of storing and restoring the user's settings, EasyGUI Provides a class called EgStore;
Case:
from easygui import EgStore
# Define a class called "Settings", inherited from the EgStore class
# Variables can be instantiated by setting values in the settings object, so that you can easily remember the settings, and then use settings.store()The method is persisted on the hard disk.
classSettings(EgStore):
def __init__(self, filename): #Need to specify file name
# Specify the name of the attribute to remember
self.author =""
self.book =""
# The following two statements must be executed
self.filename = filename
self.restore()
# Create an instantiated object "settings" of "Settings"
settingsFilename ="settings.txt"
settings =Settings(settingsFilename)
author ="WeiyiGEEK"
book ="Learn Pyhon with zero foundation"
# Save the values of the above two variables to the "settings" object
settings.author = author
settings.book = book
settings.store()print("\n Saved\n")
1.4 Catch exception
The exceptionbox() function provides a better way to handle exceptions; when using EasyGUI to write GUI programs, exceptions will inevitably occur sometimes. Of course, it depends on how you run your application. When your application crashes, the stack The trace may be thrown out or written to the stdout standard output function;
When an exception occurs, exceptionbox() will display the stack trace in a codebox() and allow you to do further processing.
for example:
import easygui
try:print('I Love FishC.com!')int('FISHC') #An exception occurs here
except:
easygui.exceptionbox() #Error processing
WeiyiGeek. Exception capture
#! /usr/bin/python3
# Case: the use of easygui module
import easygui as g
nickname =input('Please enter your screen name:')
love =input("Please enter your hobby:")
age = g.enterbox('Please enter your age:','Information input')
res = g.msgbox('The personal information you entered is:'+nickname+',Hobbies:'+love+',age:'+age,'Personal information','Confirmed')print("msgbox return value:",res) #Return ok_button值 msgbox return value: Confirmed
sex = g.buttonbox('Choose your gender:','Entry information',('male','Female','none'),default_choice='none')
city = g.multchoicebox('City selection','Entry information',['Beijing','Shanghai','Guangzhou','Shenzhen','Chongqing'])
# Select the directory to create the file
savedir = g.diropenbox(msg=None, title='',default=None)
test = g.fileopenbox(msg=None, title=None,default='*', filetypes=None, multiple=False)
savefile = g.filesavebox(msg=None, title='The directory where the file is saved',default='', filetypes=None)
# C:\Users\Administrator\Documents
# C:\Users\Administrator\Desktop\Study-Promgram\README.md
# C:\Users\Administrator\Desktop\Study-Promgram\test.txt
withopen(savefile,'w+',encoding='utf-8')as f:
f.write(nickname+'\n'+love+'\n'+age+'\n'+res+'\n'+sex+'\n')for x in city:
f.write(x+'\n')
g.textbox(nickname+'\n'+love+'\n'+age+'\n'+res+'\n'+sex+'\n','effectiveness','value')print("The module test is complete!")
WeiyiGeek.msgbox
WeiyiGeek. Execution completed
Recommended Posts