Python radio buttons, check boxes, drop-down boxes, message boxes, and file dialog boxes

Excellent tutorial: https://zhuanlan.zhihu.com/p/569960987?utm_id=0

Single line text box - Entry

  • textvariable : Single-line text box variable, String type, you can use the set and get of the variable to get or set the value
  • show : The text display mode in a single-line text box. For example, the password can be set toshow="*"
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()

Insert image description here

Multi-line text box-Text

  • width, height : width and height of the text box
  • insert(index, text) : Insert text at the specified position index
    • Index can be in the form of " line.column ", line starts from 1 and column starts from 0
    • "1.0"Represents the first row and first column (the beginning of the text), "end"represents the end of the text, and "insert"represents the insertion point of the banner.
  • delete(start, end) : delete text from start to end
  • get(start,end) : Get the text from start to end
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()

Radiobutton

  • text : The text displayed by the check box
  • variable : radio button variable, multiple radio button boxes use the same variable
    • The variable type is arbitrary (below I use text index as the value of the variable, which is the Int type)
    • You can use set and get of variables to get or set values
  • value : The value corresponding to the radio button variable
  • command : Bind the click event, triggered when the radio button is clicked
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()

Checkbox - Checkbutton

  • text : The text displayed by the check box
  • variable : checkbox variable, each checkbox variable is independent
    • Usually use Boolean type, True represents selected, False represents unselected
    • You can use set and get of variables to get or set values.
  • command : Bind click event, triggered when the check box is clicked
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()

Drop-down box - Combobox

  • values : drop-down box options
  • textvariable : drop-down box variable
    • Usually the String type is used, corresponding to the value in the drop-down box selection.
    • You can use set and get of variables to get or set values.
from tkinter import *
from tkinter.ttk import Combobox
window = Tk()
window.geometry('400x200')

def enter_handle():	 # 单击事件
    print(combobox_var.get())  # 获取值

combobox_var = StringVar()  # 下拉框变量
combobox_values = ["唱歌", "跳舞", "绘画", "编程"]  # 下拉框选项
Combobox(window, values=combobox_values, width=10, textvariable=combobox_var).pack()
combobox_var.set(combobox_values[2])  # 设置值为第三个
Button(window, text='确定', cursor="hand2", command=enter_handle).pack()
window.mainloop()

Message prompt box - messagebox

Import:from tkinter import messagebox
Insert image description here

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()

File Dialog - filedialog

Import:from tkinter import filedialog
Insert image description here

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()

Guess you like

Origin blog.csdn.net/qq_40910781/article/details/133363944