Python interface development library: PySimpleGUI

» Python » Python interface development library: PySimpleGUI

 Python interface development library: PySimpleGUI Geek Park

preface

For work reasons, Yunluo needs to develop some GUI programs here. Since the main language used by Yunluo is Python, the interface development is also in Python, and PySimpleGUI is such a simple and easy-to-use interface development library

Common GUI libraries

There are many interface libraries in Python, mainly the following

1. Tkinter: Python's built-in GUI framework, which does not need to be installed, should be its greatest advantage. Others really have no advantages. After looking at Tkinter's examples and code, it is too ugly to bear
2. PyQt5: Qt is a cross platform C++graphical user interface library. Basically, it says: niubetify, but learning is a little steep. Sometimes too much is not a good thing. Basically, this is the best way to develop advanced GUI programs
3. WxPython: wxPython is the binding of Python language to the popular wxWidgets cross platform GUI tool library. It is similar to the QT above. In fact, it is better to learn QT than play this time
4. PySimpleGUI: PySimpleGUI is Tkinter's first layer of packaging. Using PySimpleGUI to implement a custom GUI requires much less code than using Tkinter to directly write the same GUI, and it looks much better than Tkinter's programs

Putting aside dose to talk about toxicity is to play rascal. Putting aside demand to talk about development library is also to play rascal. You need to see what you do. My demand is actually to develop some small software, and the interface should not be too complex. It's too wasteful to make dumplings for a bag of vinegar. So, study this PySimpleGUI, and then find that it's really fragrant

Introducing PySimpleGUI

 Python interface development library: PySimpleGUI Geek Park

PySimpleGUI is a layer of encapsulation on the basis of Tkinter. When using psg, it can hardly feel the existence of Tkinter. There are great differences between the Tkinters he uses. I think there are mainly two. One is the layout mode, and the other is the event mode. The use of psg is also mainly about these two
 Python interface development library: PySimpleGUI Geek Park

The above are the files after the installation of psg. In fact, there are only two files. The other one is 0 dependency (Tkinter is not counted). After the installation, there are several programs in the Scripts directory, which are basically GUI programs written by the author himself
PSG supports 35 components. In fact, it has basically met the needs of daily use. If the following components do not meet your needs, then this library is not suitable for you. If your program can use the following components, then you can use PSG
 Python interface development library: PySimpleGUI Geek Park

The following is the author's GitHub feedback software
 Python interface development library: PySimpleGUI Geek Park

The above is almost a normal software style

PSG Document

Pag documents are very rich. There are materials on github, pypi and the official website. It is recommended to read all the documents in the cookbook. It usually takes about 20 minutes to read them
Document address: https://www.pysimplegui.org/en/latest/cookbook/

Use of PSG library

 #Standard import mode import PySimpleGUI as sg

There are two scenarios for using the GUI, one is temporary, one-time window pop up, which can be used directly

 #One time window event, values = sg.Window('Login Window', [[sg.T('Enter your Login ID'), sg.In(key='-ID-')], [sg.B('OK'), sg.B('Cancel') ]]).read(close=True) login_id = values['-ID-'] print(login_id)

But in fact, the most common one is a persistent GUI interface, which basically consists of six steps

 #1. Import Library import PySimpleGUI as sg #2. Drawing layout layout = [[sg.Text('Your typed chars appear here:'), sg.Text(size=(15,1), key='-OUTPUT-')], [sg.Input(key='-IN-')], [sg.Button('Show'), sg.Button('Exit')]] #3. Building windows window = sg.Window('Pattern 2B', layout) #4. Open cycle while True: event, values = window.read() #5. Write event if event in (sg.WIN_CLOSED, None, 'Exit'): break if event == 'Show': # Update the "output" text element to be the value of "input" element window['-OUTPUT-'].update(values['-IN-']) #6. Close the window window.close()

The interface presented by the above code is as follows
 Python interface development library: PySimpleGUI Geek Park

However, this is not usually the case. Cloud Cloud modifies the code as follows
 Python interface development library: PySimpleGUI Geek Park

Decompose the above code into three files, one is the main loop, one is the interface, and one is the event
The code after disassembly is as follows:
Code of run.py file

 #! / usr/bin/python3 # -*- coding: utf-8 -* import myui,myevent def run_gui(): window = myui.make_main_window() #4. Open cycle while True: event, values = window.read() #5. Write event if event in (None, 'Exit'): break myevent.apply_event(window,event,values) #6. Close the window window.close() if __name__ == '__main__': run_gui()

The myui.py file code is as follows

 #! / usr/bin/python3 # -*- coding: utf-8 -* #1. Import Library import PySimpleGUI as sg def make_main_window(): #2. Drawing layout app_label = [sg.Text('Your typed chars appear here:'), sg.Text(size=(15,1), key='-OUTPUT-')] app_input = [sg.Input(key='-IN-')] app_btn = [sg.Button('Show'), sg.Button('Exit')] layout = [ [app_label], [app_input], [app_btn] ] #3. Building windows return sg.Window('Pattern 2B', layout)

The code of myevent.py file is as follows

 #! / usr/bin/python3 # -*- coding: utf-8 -* def show_event(window, values): #Show event code window['-OUTPUT-'].update(values['-IN-']) def apply_event(window,event,values): #All interface events if event == 'Show': show_event(window, values)

But the above is only the most basic step. Let's try a ready-made software
 Python interface development library: PySimpleGUI Geek Park

It can be seen from the above that the software layout of PSG is made by using the list list. The same line is a list. The layout is automatic from left to right and from top to bottom in order. Just write the list. We write the layout above

 def make_win1(): col1 = sg.Column([ # Categories sg.Frame [sg. Frame ('file name: ', [sg. Listbox ([], key=' - dirlist - ', size=(250, 120), enable_events=True)]], size=(250, 120))], # Information sg.Frame [sg. Frame ('data file: ', [sg. Listbox ([], key=' - filelist - ', size=(250, 180), enable_events=True)]], size=(250, 180))], [sg. Frame ('measurement information: '[ [sg. T ('process: '), sg. T ('111', k='- spec -')], [sg. T ('part No.: '), sg. T ('222', k='- pn -')], [sg. T ('Batch No.: '), sg. T ('333', k='- lot -')], [sg. T ('Remark: '), sg. T ('444', k='- note -')], [sg. T ('Time: '), sg. T ('555', k='- datetime -')], [sg. T ('Other: '), sg. T ('666', k='- other -')], ], size=(250, 150))], [sg. Frame ('Throw up status: '[ [sg. T ('status: '), sg. T ('111', k='- status -')], ], size=(250, 50))], ]) col2 = sg.Column([ [sg.Frame ('Original data: ', [sg.Table ([' '* 7,' '* 7,' '* 7,' '* 7,' '* 7,' '* 7,' '* 7,' '* 7,' '* 7]], Headings=['dimension number ',' standard value ',' upper tolerance ',' lower tolerance ',' data one ',' data two '], auto_size_columns=True, max_col_width=500, size=(400, 500))]], size=(400, 500))], ]) Top_btn=[sg. Button ('open '), sg. Button ('save'), sg. Button( 'Popup'), sg. Button ('information '), sg. Button ('set'), sg. Button ('exit '),] menu_def = [['&File', ['&Open', '&Save        Ctrl-S', '&Properties', 'E&xit']], ['&Edit', ['&Paste', ['Special', 'Normal', ], 'Undo'], ], ['&Toolbar', ['---', 'Command &1', 'Command &2', '---', 'Command &3', 'Command &4']], ['&Help', '&About...'], ] TopMenu = [sg.Menu(menu_def)] Output=[sg. Frame ('real time output: ', [sg. Output (size=(92, 10))]]] statusbar = [sg.StatusBar(time.strftime('%Y/%m/%d %H:%M:%S'))] # The final layout is a simple one layout = [ [TopMenu], [top_btn], [col1, col2], [Output], [statusbar] ] Return sg. Window ('SPC uploading program for forming size ', layout,  finalize=True, disable_close=True)

Introduction to psgdemos

The author understands us! In order to facilitate our use, the author specially built a psgdemos library, which is used to bring together the software made by psg to give users "reference". If you can't write code, you can't copy code??? Take a look at the psgdemos software

 #Installation pip install psgdemos

 Python interface development library: PySimpleGUI Geek Park

You can see about 332 examples. Double click to open the software. Here's a brief look
 Python interface development library: PySimpleGUI Geek Park

 Python interface development library: PySimpleGUI Geek Park

 Python interface development library: PySimpleGUI Geek Park

 Python interface development library: PySimpleGUI Geek Park

PSG also supports theme, and can quickly switch color matching
 Python interface development library: PySimpleGUI Geek Park

Video Tutorials

This is a tutorial about pysimplegui library that Yunluo saw on station B. It looks OK. Those who are interested in it can aim at it. Address: https://space.bilibili.com/1194194498

summary

Yunluo believes that for software developers, it is not important what the underlying implementation is, but whether the contact part is simple. Although it is code layout, it is also very simple because all of it uses the list layout, which is sufficient for most software requirements

--End--

Post reply

Your email address will not be disclosed. Required items have been used * tagging