優れたチュートリアル: https://zhuanlan.zhihu.com/p/569960987?utm_id=0
単一行のテキスト ボックス - エントリ
- textvariable : 単一行のテキスト ボックス変数、文字列型。変数の set および get を使用して値を取得または設定できます。
- show : 単一行テキストボックスのテキスト表示モード。たとえば、パスワードを次のように設定できます。
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()
複数行のテキスト ボックス - テキスト
- width, height : テキストボックスの幅と高さ
- insert(index, text) : 指定した位置にテキストを挿入します。
- インデックスは「 line.column 」の形式で、行は 1 から始まり、列は 0 から始まります。
"1.0"
最初の行と最初の列 (テキストの先頭) を表し、"end"
テキストの終わりを表し、"insert"
バナーの挿入ポイントを表します。
- delete(start, end) : テキストを最初から最後まで削除します。
- get(start,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()
ラジオボタン
- text : チェックボックスに表示されるテキスト
- variable : ラジオ ボタン変数、複数のラジオ ボタン ボックスは同じ変数を使用します
- 変数の型は任意です (以下では、変数の値としてテキスト インデックスを使用します。これは Int 型です)。
- 変数の set および get を使用して値を取得または設定できます
- value : ラジオボタン変数に対応する値
- command : ラジオボタンがクリックされたときにトリガーされるクリックイベントをバインドします。
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()
チェックボックス - チェックボタン
- text : チェックボックスに表示されるテキスト
- 変数: チェックボックス変数、各チェックボックス変数は独立しています
- 通常はブール型を使用します。True は選択されていることを表し、False は選択されていないことを表します
- 変数の set および get を使用して、値を取得または設定できます。
- command : バインドクリックイベント、チェックボックスがクリックされたときにトリガーされます
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()
ドロップダウン ボックス - コンボボックス
- 値: ドロップダウン ボックスのオプション
- textvariable : ドロップダウン ボックス変数
- 通常は、ドロップダウン ボックスの選択の値に対応する String タイプが使用されます。
- 変数の set および get を使用して、値を取得または設定できます。
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()
メッセージプロンプトボックス - メッセージボックス
輸入: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()