foreword
We used Python to update the attributes of song files in batches before, but in order to allow more people to use this tool conveniently, a more friendly graphical interface is needed. For this, I chose to use the Tkinter module to create a simple and easy-to-use graphical interface.
Historical article reference:
- [Python practical skills] How to modify song information in batches
- [Python practical skills] How to batch modify the Mutagen library of song information
1 Introduction to Tkinter
The tkinter package ("Tk interface") is the standard Python interface to the Tcl/Tk GUI toolkit. Tk and tkinter are available on most Unix platforms, including macOS, and on Windows.
Since Tkinter is built into the python installation package, the Tkinter library can be imported after Python is installed, and IDLE is also written in Tkinter, so Tkinter can handle the simple graphical interface with ease. For detailed instructions, please refer to the official documentation .
2 Tkinter main knowledge
2.1 Tkinter components
Tkinter provides various controls, such as buttons, labels and text boxes, for use in a GUI application. These controls are often referred to as controls or widgets.
There are currently 15 widgets for Tkinter. We present these components along with a short introduction in the table below:
control | describe |
---|---|
Button | Button control; displays a button in a program. |
Canvas | Canvas control; displays graphical elements such as lines or text |
Checkbutton | Multiple selection box control; used to provide multiple selection boxes in the program |
Entry | Input controls; used to display simple text content |
Frame | Frame control; display a rectangular area on the screen, mostly used as a container |
Label | Label control; can display text and bitmaps |
Listbox | Listbox control; the Listbox widget is used to display a list of strings to the user |
Menu button | A menu button control, used to display menu items. |
Menu | Menu control; display menu bar, drop-down menu and pop-up menu |
Message | Message control; used to display multi-line text, similar to label |
Radiobutton | Radio button control; displays the state of a radio button |
Scale | Range control; displays a numeric scale that limits the range of numeric intervals for output |
Scrollbar | A scroll bar control, used when the content exceeds the visible area, such as a list box. . |
Text | Text control; used to display multiple lines of text |
Toplevel | Container control; used to provide a separate dialog box, similar to Frame |
Spinbox | Input control; similar to Entry, but you can specify the input range value |
PanedWindow | PanedWindow is a plug-in for window layout management, which can contain one or more sub-controls. |
LabelFrame | labelframe is a simple container control. Common and complex window layouts. |
tkMessageBox | Used to display message boxes for your application. |
2.2 Standard properties
Standard properties are the common properties of all controls, such as size, font and color, and so on.
Attributes | describe |
---|---|
Dimension | control size; |
Color | control color; |
Font | control font; |
Anchor | anchor point; |
Relief | control style; |
Bitmap | bitmap; |
Cursor | cursor; |
2.3 Geometry Management
Tkinter controls have a specific geometric state management method to manage the entire control area organization. The following are the geometry management classes exposed by Tkinter: package, grid, position
geometric method | describe |
---|---|
pack() | Package; |
grid() | grid; |
place() | Location; |
3 Tkinter programming
The detailed code implementation is as follows:
import os
import csv
import tkinter as tk
import time
from datetime import datetime
import mutagen
from mutagen.id3 import ID3, TIT2, TPE1, TPE1, TALB, TDRC, TCON
class MusicInfoGUI:
def __init__(self):
self.window = tk.Tk()
self.window.title("音乐信息管理系统")
self.window.geometry("800x300")
self.btn1 = tk.Button(self.window, text="1、点击读取当前路径下所有歌曲文件信息", command=self.read_music_files)
self.btn1.pack(pady=20)
self.btn2 = tk.Button(self.window, text="2、更新完music.csv后点击修改歌曲属性信息", command=self.update_music_info)
self.btn2.pack(pady=20)
self.label = tk.Label(self.window, text="")
self.label.pack(pady=20)
self.window.mainloop()
def read_music_files(self): ... # 代码参考前文
def update_music_info(self):... # 代码参考前文
if __name__ == "__main__":
app = MusicInfoGUI()