当前位置:   article > 正文

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

python gui设计(tkinter菜鸟编程) pdf

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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/124855
推荐阅读
相关标签
  

闽ICP备14008679号