# written by wangluojisuan
import tkinter
from tkinter import messagebox
global main_form
global lbl_name
global entry_name
global entry_text_var
global chk_var
global chk
global text_area
def window_quit():
if tkinter.messagebox.askyesno("提示", "退出?"):
main_form.quit()
def btn_click(event):
str_entry = entry_name.get()
text_area.insert('end', '%s,%s\n'%(str_entry, entry_text_var.get()))
def chk_select():
text_area.insert('end', '%s\n'%(chk_var.get()))
if __name__ == '__main__':
# 窗体
main_form = tkinter.Tk() # 主窗体
main_form.wm_title('tkinter的演示程序') # 修改窗体标题
# 标签
lbl_name = tkinter.Label(main_form, text='姓名:', background='red')
# 添加标签
lbl_name.grid(row=0, column=0, sticky='w', ipadx=10, padx=20) # 设置标签位置
# 输入框
entry_text_var = tkinter.StringVar()
entry_text_var.set('默认值')
entry_name = tkinter.Entry(main_form, textvariable=entry_text_var)
entry_name.grid(row=0, column=1)
# 复选框
chk_var = tkinter.IntVar()
chk = tkinter.Checkbutton(main_form, text='附加', variable=chk_var, command=chk_select)
chk.grid(row=0, column=2)
# 按钮
btn = tkinter.Button(main_form, text='点击')
btn.grid(row=0, column=3, ipadx=10, padx=10)
btn.bind('', btn_click)
# 文本域
text_area = tkinter.Text(main_form)
text_area.grid(row=1, columnspan=4)
# 窗体关闭事件
main_form.protocol('WM_DELETE_WINDOW', window_quit)
main_form.mainloop()