概述
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()
转载于:https://www.cnblogs.com/baijifeilong/p/3707393.html
最后
以上就是漂亮魔镜为你收集整理的Python -- Gui编程 -- Tkinter的使用 -- 对话框消息框的全部内容,希望文章能够帮你解决Python -- Gui编程 -- Tkinter的使用 -- 对话框消息框所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复