About assessment:
I have roughly sorted out the difficulties of the assessment task, hoping to break them in the next three weeks.Copy the code
- 1. Enter a song name and check whether the song exists in the database. If not, return a list of search results by song title (tentative first two pages) and display them on the page produced by Tkinter
- 2. Select Single Download/Batch Download, obtain the download link, and download
- 3. Store the download results in the format of song name-singer-ID in the database
Today I learned:
Layout manager (I) : Pack
- 1. Arrange components in order of addition, used for arrangement of a small number of components, the same as the other two layout managers, used to manage the layout of all components under the same parent component, but careful not to mix Pack and grid in the same parent component
- 2. By default, the components added to Pack are arranged vertically
import tkinter as tk
root=tk.Tk()
tk.Label(root,text='Red'.bg='red'.fg='white').pack(fill='x')
tk.Label(root,text='Green'.bg='green'.fg='black').pack(fill='x')
tk.Label(root,text='Blue'.bg='blue'.fg='white').pack(fill='x')
root.mainloop()
Copy the code
- 3. To arrange components horizontally, set the Side option
import tkinter as tk
root=tk.Tk()
tk.Label(root,text='Red'.bg='red'.fg='white').pack(side='right')
tk.Label(root,text='Green'.bg='green'.fg='black').pack(side='right')
tk.Label(root,text='Blue'.bg='blue'.fg='white').pack(side='right')
root.mainloop()
Copy the code
Component (1) : Label
- 1. The Label component is used to display text or images. Only text in a single font can be displayed, but text can span multiple lines
- 2. If the size is not specified, then the size of the Label is just enough to hold the content
import tkinter as tk
root=tk.Tk()
w=tk.Label(root,text='Hello, RDC.'.fg='green').pack()
root.mainloop()
Copy the code
- 3. Display multiple lines of text:
import tkinter as tk
root=tk.Tk()
longtext = """Label can display multiple lines of text. You can do this either by using a newline character or by using the Wraplength option. When text wraps, you can use the Anchor and justify options to make the text appear as you want it to. """
w=tk.Label(root,text=longtext,anchor="w",justify='left').pack()
root.mainloop()
Copy the code
- 4. The contents of the Tkinter variable can be displayed
import tkinter as tk
root=tk.Tk()
v=tk.StringVar()
w=tk.Label(root,textvariable=v).pack()
v.set('Hello RDC')
root.mainloop()
Copy the code
(2). The Button:
- 1. The Button component can contain text or images, and you can associate a Python function or method with it, which is automatically executed when the Button is pressed. The Button component can only display text in a single font, but text can span multiple lines
- 2. Correlation function:
import tkinter as tk
root=tk.Tk()
def func():
print('Hello RDC')
b=tk.Button(root,text='execution'.command=func).pack()
root.mainloop()
Copy the code
- 3. Multi-line text is used as Label
- 4. To make the button concave when clicked, you can set the Settings:
b.config(relief="raised")
Copy the code
3. Checkbutton:
- 1. Button used to implement either of the two. The Checkbutton component can contain text or images, and you can associate a Python function or method with it, which will be automatically executed when the button is pressed. The Checkbutton component can only display text in a single font, but text can span multiple lines.
- 2. To use Checkbutton, you must create a Tkinter variable to store the state of the button
import tkinter as tk
root=tk.Tk()
var=tk.IntVar()
c=tk.Checkbutton(root,text='Hello RDC',variable=var).pack()
root.mainloop()
Copy the code
- 3. By default, the variable option is set to 1 to indicate that the variable is selected. Otherwise, the variable option is set to 0. You can use the onValue and offValue options to change their values, as in the following code, as long as var is set to “T” it is checked, and as long as var is set to “F” it is not:
import tkinter as tk
root=tk.Tk()
var=tk.StringVar()
c=tk.Checkbutton(root,text='Hello RDC',variable=var,onvalue='T',offvalue='F').pack()
var.set('T')
root.mainloop()
Copy the code
4. Radiobutton:
- 1. The problem of realizing multiple choices. The Radiobutton component can contain text or images, and each button can be associated with a Python function or method that is automatically executed when the button is pressed. The Radiobutton component can only display text in a single font, but text can span multiple lines.
- 2. To implement radio behavior, make sure all buttons in a group use the same variable for their variable options, and use the value option to specify what value each button represents
import tkinter as tk
root=tk.Tk()
v=tk.IntVar()
def func():
print('Hello RDC')
tk.Radiobutton(root,text='one',variable=v,value=1,command=func).pack(anchor='w')
tk.Radiobutton(root,text='two',variable=v,value=2,command=func).pack(anchor='w')
tk.Radiobutton(root,text='three',variable=v,value=3,command=func).pack(anchor='w')
v.set(2)
root.mainloop()
Copy the code
- 3. If there are too many button options, use nested tuples in the list to iterate over assignments:
import tkinter as tk
root=tk.Tk()
girls=[
("Beauty", 1),
("Wang Zhaojun", 2),
("The sable cicada"And 3), ("Yang Yuhuan", 4)]
v=tk.IntVar()
for girl,num in girls:
b=tk.Radiobutton(root,text=girl,variable=v,value=num).pack(anchor='w')
root.mainloop()
Copy the code
- 4. Radiobutton becomes a normal button if the Indicatoron option is set to False
5. Frame:
- 1. Used to group other components in complex layouts, also for padding spacing and as a base class for implementing advanced components
- 2. Decorate the interface
import tkinter as tk
root=tk.Tk()
tk.Label(text='The King of Heaven covers the Tiger.').pack()
separator=tk.Frame(height=2,bd=1,relief='sunken')
separator.pack(fill='x',padx=5,pady=5)
tk.Label(text='Chicken and Mushroom stew').pack()
root.mainloop()
Copy the code
6. LabelFrame:
- 1. Used to group components. By default, LabelFrame draws a border and a title around its children.
- 2. Example:
import tkinter as tk
root=tk.Tk()
group=tk.LabelFrame(root,text='Where do you know about RDC?',padx=5,pady=5)
group.pack(padx=10,pady=10)
v=tk.IntVar()
r1=tk.Radiobutton(group,text='Classmate Introduction',variable=v,value=1).pack(anchor='w')
r1=tk.Radiobutton(group,text='Teacher introduction',variable=v,value=2).pack(anchor='w')
root.mainloop()
Copy the code
Seven. Entry:
- 1. Obtain the user input text
- 2. Insert () is used to add text, delete() is used to delete text, and get() is used to obtain text. The index number “end” corresponds to the last position of the existing text, and “INSERT” corresponds to the current position of the insert cursor
import tkinter as tk
root=tk.Tk()
e=tk.Entry(root)
e.pack(padx=20,pady=20)
e.delete(0,'end') Delete all the contents of the input box
e.insert(0,'Please enter content')
print(e.get())
root.mainloop()
Copy the code
- 3. You can bind the Entry component to the Tkinter variable (StringVar) and use this variable to set and get the text of the input box:
import tkinter as tk
root=tk.Tk()
v=tk.StringVar()
e=tk.Entry(root,textvariable=v)
e.pack()
v.set('I'm going into RDC.')
print(v.get())
root.mainloop()
Copy the code
- 4. Example: Combine Entry component and Button component, click “Get Information” Button to automatically clear the input box and output the content:
import tkinter as tk
root=tk.Tk()
tk.Label(root,text='name').grid(row=0)
tk.Label(root,text='Studio').grid(row=1)
e1=tk.Entry(root)
e2=tk.Entry(root)
e1.grid(row=0,column=1,padx=10,pady=5)
e2.grid(row=1,column=1,padx=10,pady=5)
def func():
print('{} to join {} studio '.format(e1.get(),e2.get()))
e1.delete(0,'end')
e2.delete(0,'end')
tk.Button(root,text='Get information',width=10,command=func).grid(row=3,column=0,sticky='w',padx=10,pady=5)
tk.Button(root,text='exit',width=10,command=root.quit).grid(row=3,column=1,sticky='e',padx=10,pady=5)
root.mainloop()
Copy the code