In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article introduces "how to use a line of Python code to achieve GUI graphical interface" related knowledge, in the actual case of the operation process, many people will encounter such a dilemma, and then let the editor lead you to learn how to deal with these situations! I hope you can read it carefully and be able to achieve something!
You mainly use Python's PySimpleGUI library to do this.
# install PySimpleGUIpip install PySimpleGUI-I https://mirror.baidu.com/pypi/simple1 and select a folder
First import the PySimpleGUI library and use the abbreviation sg to represent it.
The import PySimpleGUI as sg # window displays text boxes and browse buttons to select a folder dir_path = sg.popup_get_folder ("Select Folder") if not dir_path: sg.popup ("Cancel", "No folder selected") raise SystemExit ("Cancelling: no folder selected") else: sg.popup ("The folder you chose was", dir_path)
By using the popup_get_folder () method of PySimpleGUI, you can select a folder with a single line of code.
Examples are as follows
Click the Browse button, select the folder, and the text box will show the absolute path to the folder.
Click the OK button to display the final selected path information, and click the OK button again to end the window.
If you do not select a folder, but directly click the OK button, it will directly prompt that the folder is not selected.
2. Select a file
The file selection operation is a bit similar to the folder selection above.
# window displays a text box and browse button to select the file fname = sg.popup_get_file ("Choose Excel file", multiple_files=True, file_types= (("Excel Files", "* .xls *"),),) if not fname: sg.popup ("Cancel", "No filename supplied") raise SystemExit ("Cancelling: no filename supplied") else: sg.popup ("The filename you chose was", fname)
The difference is that selecting a file sets the multiple_files (whether it is multiple files) and file_types (file type) parameters.
Examples are as follows
Multiple Excel files are selected, and the final result returns the path addresses of all files.
3. Select a date
Using the popup_get_date () method, a calendar window is displayed.
# displays a calendar window and returns a tuple (month, day, year) date = sg.popup_get_date () if not date: sg.popup ("Cancel", "No date picked") raise SystemExit ("Cancelling: no date picked") else: sg.popup ("The date you chose was", date)
Examples are as follows
After selecting the date, click the OK button to return the date tuple result.
4. Enter text
Use the popup_get_text () method to display a text input box.
# display the text input box, enter the text information, and return the entered text. If you cancel, return Nonetext = sg.popup_get_text ("Please enter a text:") if not text: sg.popup ("Cancel", "No text was entered") raise SystemExit ("Cancelling: no text entered") else: sg.popup ("You have entered", text)
Type information. Examples are as follows
Click the OK button to return the entered text information.
If there is no input, click the OK button directly, it will prompt that there is no text input.
5. Pop-up window without button # displays a pop-up window, but does not have any button sg.popup_no_buttons ("You cannot click any buttons")
The results are as follows
6. Pop-up window without title # displays a pop-up window sg.popup_no_titlebar without title bar ("A very simple popup")
The results are as follows
7. Pop-up window only has OK button # shows pop-up window and only OK button sg.popup_ok ("You can only click on 'OK'")
The results are as follows
8. Pop-up window only Error button (red) # shows pop-up window and only error button, button with color sg.popup_error ("Something went wrong")
The results are as follows
9. Display the notification window # displays a "notification window", usually in the lower right corner of the screen, and the window will slowly fade in and out of the sg.popup_notify ("Task done!")
As a result, the Task done prompt fades in and out.
10. Select # pop-up window and yes or no button, and select answer = sg.popup_yes_no ("Do you like this video?") sg.popup ("You have selected", answer)
The results are as follows
11. Custom pop-up windows
The above pop-up windows are included in the library. If you want to customize the creation, you can refer to the following method.
# Custom create pop-up window, and complete choice with one line of code, _ = sg.Window ("Continue?", [[sg.T ("Do you want to subscribe to this channel?")], [sg.Yes (read 10), sg.No (sg.No (disable_close=True 10)]], disable_close=True,) .read (close=True) sg.popup ("Your choice was", choice)
The results are as follows
12. Actual combat
Finally, a comprehensive practical case, all the Excel files in a folder in the sheet table, one by one as a separate Excel file.
The code is as follows, and you need to install the xlwings library, where the pathlib library is built-in.
From pathlib import Pathimport PySimpleGUI as sgimport xlwings as xw # Select the input folder INPUT_DIR = sg.popup_get_folder ("Select an input folder") if not INPUT_DIR: sg.popup ("Cancel") "No folder selected") raise SystemExit ("Cancelling: no folder selected") else: INPUT_DIR = Path (INPUT_DIR) # Select output folder OUTPUT_DIR = sg.popup_get_folder ("Select an output folder") if not OUTPUT_DIR: sg.popup ("Cancel") "No folder selected") raise SystemExit ("Cancelling: no folder selected") else: OUTPUT_DIR = Path (OUTPUT_DIR) # get the path list of all xls format files in the input folder files = list (INPUT_DIR.rglob ("* .xls *") with xw.App (visible=False) as app: for index, file in enumerate (files): # display progress sg.one_line_progress_meter ("Current Progress", index + 1) Len (files) wb = app.books.open (file) # extract the sheet table as a separate Excel table for sheet in wb.sheets: wb_new = app.books.add () sheet.copy (after=wb_new.sheets [0]) wb_new.sheets [0] .delete () wb_new.save (OUTPUT_DIR / f "{ File.stem} _ {sheet.name} .xlsx ") wb_new.close () sg.popup_ok (" Task done! ")
First select the address of the input folder and the output folder.
Then traverse the input folder through the pathlib library to find out the path addresses of all the files in xls format.
After clicking the OK button, the table conversion will begin, as follows.
The one_line_progress_meter () method is used to show the progress of the program processing.
20 means there are 20 loops, there are a total of 20 original Excel files that need to be processed 20 times, and the rest are shown in the figure above.
This is the end of the introduction of "how to use a line of Python code to implement the GUI graphical interface". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.