分享

Tkinter教程之tkCommonDialog篇

 Mq_Guo 2017-07-23
[python] view plain copy
  1. '''''2.使用tkSimpleDialog模块'''  
  2. # askinteger:输入一个整数值  
  3. # askfloat:输入一个浮点数  
  4. # askstring:输入一个字符串  
  5. from tkinter import *  
  6. # 引入SimpleDialog模态对话框  
  7. from tkinter.simpledialog import *  
  8.   
  9. root = Tk()  
  10. # 输入一个整数,  
  11. # initialvalue指定一个初始值  
  12. # prompt提示信息  
  13. # title提示框标题  
  14. print(askinteger(title='prompt', prompt='input a integer:', initialvalue=100))  
  15. # 输入一浮点数  
  16. # minvalue指定最小值  
  17. # maxvalue指定最大值,如果不在二者指定范围内则要求重新输入  
  18. print(askfloat(title='float', prompt='input a float', minvalue=0, maxvalue=11))  
  19. # 输入一字符串  
  20. print(askstring(title='string', prompt='input a string'))  
  21. root.mainloop()  
  22. # 返回值为各自输入的值。  

[python] view plain copy
  1. '''''2.打开文件对话框'''  
  2. # LoadFileDialog:打开对话框  
  3. from tkinter import *  
  4. from tkinter.filedialog import *  
  5.   
  6. root = Tk()  
  7. # 指定master就可以了。  
  8. # title属性用来指定标题  
  9. fd = LoadFileDialog(root)  
  10. # go方法的返回值即为选中的文本路径,如果选择取返回值则为None  
  11. print(fd.go())  
  12. root.mainloop()  
  13. # 返回选中的文件名称  

[python] view plain copy
  1. '''''3.保存文件对话框'''  
  2. # SaveFileDialog:保存对话框  
  3. # 与LoadFileDialog正好操作相反,这个类是用来保存文件。  
  4. # 各个 参数的意义都  一样,只是ok的返回值为保存的文件名称;如果取消则为None  
  5. from tkinter import *  
  6. from tkinter.filedialog import *  
  7.   
  8. root = Tk()  
  9. # 指定master就可以了。  
  10. # title属性用来指定标题  
  11. fd = SaveFileDialog(root)  
  12. # go方法的返回值即为选中的文本路径,如果选择取返回值则为None  
  13. print(fd.go())  
  14. root.mainloop()  
  15. # 返回选中的文件名称  

[python] view plain copy
  1. '''''4.使用颜色对话框'''  
  2. # askcolor:颜色对话框  
  3. from tkinter import *  
  4. # 引入tkColorChoose模块  
  5. from tkinter.colorchooser import *  
  6.   
  7. root = Tk()  
  8.   
  9. # 调用askcolor返回选中颜色的(R,G,B)颜色值及#RRGGBB表示  
  10. print(askcolor())  
  11. root.mainloop()  
  12. # 返回选中的文件名称  

[python] view plain copy
  1. '''''5. 使用消息对话框'''  
  2. # -*- coding: utf-8 -*-  
  3. # showinfo:信息对话框  
  4. # showwarning:警告对话框  
  5. # showerror:错误对话框  
  6. # showquestion:询问对话框  
  7. # showokcancel:显示确定/取消对话框  
  8. # showyesno:是/否对话框  
  9. # showretrycancel:重试/取消对话框  
  10. # 使用提示对话框模块tkMessageBox  
  11. from tkinter import *  
  12. # 引入tkMessageBox模块  
  13. from tkinter.messagebox import *  
  14.   
  15. root = Tk()  
  16. stds = [  
  17.     showinfo,  # 显示信息消息框  
  18.     showwarning,  # 显示警告消息框  
  19.     showerror,  # 显示错误消息框  
  20.     askquestion,  # 显示询问消息框  
  21.     askokcancel,  # 显示确认/取消消息框  
  22.     askyesno,  # 显示是/否消息框  
  23.     askretrycancel  # 显示重试/取消消息框  
  24. ]  
  25. for std in stds:  
  26.     print(str(std), std(title=str(std), message=str(std)))  
  27. # 程序打印输出结果如下(与点击的按钮得到不同其值)  
  28. # <function showinfo at 0x00D589F0> ok  
  29. # <function showwarning at 0x00D58A30> ok  
  30. # <function showerror at 0x00D58A70> ok  
  31. # <function askquestion at 0x00D58AB0> yes  
  32. # <function askokcancel at 0x00D58AF0> False  
  33. # <function askyesno at 0x00D58B30> True  
  34. # <function askretrycancel at 0x00D58B70> True  
  35. root.mainloop()  
  36. # 如果要确认点击的是那一个按钮,则可以判断这个消息框的返回值,注意各个值有所不同  
  37. # 返回值有ok/yes/True  

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多