1.新建一个窗口
import tkinter as tk
window = tk.Tk() #窗口组件
window.title('exam') #窗口标签
window.geometry('300x200') #窗口大小
window.mainloop()
2.新建标签组件--Label
l1 = tk.Label(window,text='example_label',background='pink',fg='green',font=('Arial',12),width=20,height=2) #标签组件
l1.pack() #放置标签
3.新建按钮组件--Button
strvar = tk.StringVar()
l2 = tk.Label(window,textvariable=strvar,background='pink',fg='green',font=('Arial',12),width=20,height=2) #标签组件
l2.pack() #放置标签
flag = False
def click():
global flag
if flag == False:
flag = True
strvar.set('you have clicked me!')
else:
flag = False
strvar.set('')
b1 = tk.Button(window,text='example_button',fg='red',font=('Arial',12),width=20,height=2,command=click) #按钮组件
b1.pack() #放置按钮
点击按钮,Label文字发生改变
4.新建文本输入框--Entry
e1 = tk.Entry(window, show=None, font=('Arial', 14)) #账号输入文本框
e2 = tk.Entry(window, show='*', font=('Arial', 14)) #密码输入文本框
e1.pack() #放置文本框
e2.pack()
5.用grid函数放置组件
for i in range(3):
for j in range(3):
tk.Label(window, text='❤').grid(row=i, column=j, padx=10, pady=10, ipadx=10, ipady=10)
6.新建提示消息窗口--messagebox
import tkinter.messagebox as tm
def click():
tm.showinfo(title='HELP', message='❤') #提示窗口组件
tk.Button(window, text='click', bg='green', font=('Arial', 14), command=click).pack()
其余组件操作类似……