1 # 简单对话框,包括字符、整数和浮点数 2 import tkinter as tk 3 from tkinter import simpledialog 4 5 6 def input_str(): 7 r = simpledialog.askstring('字符录入', '请输入字符', initialvalue='hello world!') 8 if r: 9 print(r) 10 label['text'] = '输入的是:' + r 11 12 13 def input_int(): 14 r = simpledialog.askinteger('整数录入', '请输入整数', initialvalue=100) 15 if r: 16 print(r) 17 label['text'] = '输入的是:' + str(r) 18 19 20 def input_float(): 21 r = simpledialog.askfloat('浮点数录入', '请输入浮点数', initialvalue=1.01) 22 if r: 23 print(r) 24 label['text'] = '输入的是:' + str(r) 25 26 27 root = tk.Tk() 28 root.title('对话框') 29 root.geometry('300x100+300+300') 30 31 label = tk.Label(root, text='输入对话框,包括字符、整数和浮点数', font='宋体 -14', pady=8) 32 label.pack() 33 34 frm = tk.Frame(root) 35 btn_str = tk.Button(frm, text='字符', width=6, command=input_str) 36 btn_str.pack(side=tk.LEFT) 37 btn_int = tk.Button(frm, text='整数', width=6, command=input_int) 38 btn_int.pack(side=tk.LEFT) 39 btn_int = tk.Button(frm, text='浮点数', width=6, command=input_float) 40 btn_int.pack(side=tk.LEFT) 41 frm.pack() 42 43 root.mainloop()
截图: