Python -- Gui编程 -- Tkinter的使用 -- 对话框消息框

https://www.cnblogs.com/baijifeilong/p/3707393.html

1.消息框

tkMessageBox.py

复制代码
 1 import tkinter
 2 from tkinter import messagebox
 3 
 4 def cmd():
 5     global n
 6     global buttontext
 7     n += 1
 8     if n==1:
 9         messagebox.askokcancel('Python Tkinter', 'askokcancel')
10         buttontext.set('askquestion')
11     elif n==2:
12         messagebox.askquestion('Python Tkinter', 'askquestion')
13         buttontext.set('askyesno')
14     elif n==3:
15         messagebox.askyesno('Python Tkinter', 'askyesno')
16         buttontext.set('showerror')
17     elif n==4:
18         messagebox.showerror('Python Tkinter', 'showerror')
19         buttontext.set('showinfo')
20     elif n==5:
21         messagebox.showinfo('Python Tkinter', 'showinfo')
22         buttontext.set('showwarning')
23     else:
24         n = 0
25         messagebox.showwarning('Python Tkinter', 'showwarning')
26         buttontext.set('askokcancel')
27 
28 n = 0
29 root = tkinter.Tk()
30 buttontext = tkinter.StringVar()
31 buttontext.set('askokcancel')
32 button = tkinter.Button(root, textvariable=buttontext, command=cmd)
33 button.pack()
34 root.mainloop()
复制代码

2.简单对话框

tkSimpleDialog.py

复制代码
 1 import tkinter
 2 from tkinter import simpledialog
 3 
 4 def inputStr():
 5     r = simpledialog.askstring('Python Tkinter', 'Input String', initialvalue = 'Python Tkinter')
 6     print(r)
 7 def inputInt():
 8     r = simpledialog.askinteger('Python Tkinter', 'Input Integer')
 9     print(r)
10 def inputFloat():
11     r = simpledialog.askfloat('Python Tkinter', 'Input Float')
12     print(r)
13 
14 root = tkinter.Tk()
15 btn1 = tkinter.Button(root, text='Input String', command=inputStr)
16 btn2 = tkinter.Button(root, text='Input Integer', command=inputInt)
17 btn3 = tkinter.Button(root, text='Input Float', command=inputFloat)
18 
19 btn1.pack(side='left')
20 btn2.pack(side='left')
21 btn3.pack(side='left')
22 
23 root.mainloop()
复制代码

3.文件对话框

tkFileDialog.py

复制代码
 1 import tkinter
 2 from tkinter import filedialog
 3 
 4 def openfile():
 5     r = filedialog.askopenfilename(title='打开文件', filetypes=[('Python', '*.py *.pyw'), ('All Files', '*')])
 6     print(r)
 7 def savefile():
 8     r = filedialog.asksaveasfilename(title='保存文件', initialdir='d:\mywork', initialfile='hello.py')
 9     print(r)
10 
11 root = tkinter.Tk()
12 btn1 = tkinter.Button(root, text='File Open', command=openfile)
13 btn2 = tkinter.Button(root, text='File Save', command=savefile)
14 
15 btn1.pack(side='left')
16 btn2.pack(side='left')
17 root.mainloop()
复制代码

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值