优秀教程:https://zhuanlan.zhihu.com/p/569960987?utm_id=0
from tkinter import *
window = Tk()
window.geometry('400x200')
def enter_handle(): # 单击事件
print(label["text"]) # 获取值
label["text"] = "嘻嘻嘻" # 修改值
label = Label(window, text="哈哈哈")
label.pack()
button = Button(window, text='确定', cursor="hand2", command=enter_handle)
button.pack()
window.mainloop()
show="*"
from tkinter import *
window = Tk()
window.geometry('400x200')
def enter_handle(): # 打印事件
print("账号:", username.get()) # 获取单行文本框内容
print("密码:", password.get())
def clear_handle(): # 清空事件
username.set("") # 设置单行文本框内容
password.set("")
"""账号框架"""
username_frame = Frame(window)
Label(username_frame, text="账号:").pack(side=LEFT) # 组件按照自左向右方向排列
username = StringVar() # 与单行文本框绑定的变量
Entry(username_frame, textvariable=username).pack(side=LEFT)
username_frame.pack(pady=5) # 组件之间的水平间距
"""密码框架"""
password_frame = Frame(window)
Label(password_frame, text="密码:").pack(side=LEFT)
password = StringVar()
Entry(password_frame, textvariable=password, show="*").pack(side=LEFT)
password_frame.pack(pady=5)
"""按钮框架"""
button_frame = Frame(window)
Button(button_frame, text='确定', cursor="hand2", width=5, command=enter_handle).pack(side=LEFT, padx=10)
Button(button_frame, text='清空', cursor="hand2", width=5, command=clear_handle).pack(side=LEFT, padx=10)
button_frame.pack()
window.mainloop()
"1.0"
代表第一行第一列(文本开始),"end"
代表文本最后,"insert"
代表广标插入处from tkinter import *
window = Tk()
window.geometry('400x200')
def enter_handle():
print(my_text.get("1.0", "end")) # 获取值
my_text = Text(window, height=5, width=20)
my_text.insert("1.0", "哈哈哈") # 从开头插入值
my_text.insert("end", "嘻嘻嘻") # 从最后插入值
my_text.pack()
Button(window, text='确定', cursor="hand2", command=enter_handle).pack()
Button(window, text='清空', cursor="hand2", command=lambda: my_text.delete("1.0", "end")).pack() # 清空值
Button(window, text='插入', cursor="hand2", command=lambda: my_text.insert("insert", "666")).pack() # 在光标处插入值
window.mainloop()
from tkinter import *
window = Tk()
window.geometry('400x200')
def radio_handle(): # 单击任意单元框时触发
print("哈哈哈哈")
def enter_handle(): # 单击事件
print(radio_var.get()) # 获取单元框的值
print(radio_texts[radio_var.get()]) # 获取单元框的文本
radio_texts = ["Java", "Python", "C++", "PHP"] # 单选框文本
radio_var = IntVar() # 单选框变量
for index, text in enumerate(radio_texts):
Radiobutton(window, text=text, variable=radio_var, value=index, command=radio_handle).pack()
radio_var.set(2) # 设置第三个单元框选中
Button(window, text='确定', cursor="hand2", command=enter_handle).pack()
window.mainloop()
from tkinter import *
window = Tk()
window.geometry('400x200')
def check_handle(): # 单击任意复选框时触发
print("哈哈哈哈")
def enter_handle(): # 单击事件
check_selects = []
for index, var in enumerate(check_vars):
if var.get(): # 获取复选框状态,True代表选中,False代表未选中
check_selects.append(check_texts[index])
print(check_selects) # 打印出所有的选择项
check_texts = ["唱歌", "跳舞", "绘画", "编程"] # 复选框文本
check_vars = [BooleanVar() for i in check_texts] # 复选框变量
for index, text in enumerate(check_texts):
Checkbutton(window, text=text, variable=check_vars[index], command=check_handle).pack()
check_vars[1].set(True) # 设置第二个复选框选中
check_vars[3].set(True) # 设置第四个复选框选中
Button(window, text='确定', cursor="hand2", command=enter_handle).pack()
window.mainloop()
from tkinter import *
from tkinter.ttk import Combobox
window = Tk()
window.geometry('400x200')
def enter_handle(): # 单击事件
print(combobox.get()) # 获取值
def combobox_selected(event): # 下拉框选择时触发
print(combobox.get()) # 获取值
combobox = Combobox(window, values=["唱歌", "跳舞", "绘画", "编程"], width=10)
combobox.bind("<>" , combobox_selected) # 绑定下拉框选择事件
combobox.pack()
combobox.current(2) # 设置值为第三个
Button(window, text='确定', cursor="hand2", command=enter_handle).pack()
window.mainloop()
导入:from tkinter import messagebox
from tkinter import *
from tkinter import messagebox
window = Tk()
window.geometry('400x200')
def enter_handle():
messagebox.showinfo("标题","我是消息提示框")
Button(window, text='确定', cursor="hand2", command=enter_handle).pack()
window.mainloop()
导入:from tkinter import filedialog
from tkinter import *
from tkinter import filedialog
window = Tk()
window.geometry('400x200')
def enter_handle():
file = filedialog.askdirectory()
if file is not None:
print(file.name) # 打印文件绝对路径
Button(window, text='确定', cursor="hand2", command=enter_handle).pack()
window.mainloop()