Tkinter教程之tkCommonDialog篇

来源:互联网 发布:瞩目zhumu视频会议软件 编辑:程序博客网 时间:2024/05/16 09:56
'''Tkinter教程之tkCommonDialog篇'''
'''1.使用用模态对话框SimpleDialg'''
# SimpleDialog:创建一个模态对话框
from Tkinter import *
# 引入SimpleDialog模态对话框
from SimpleDialog import *

root 
= Tk()
# 创建一个SimpleDialog
#
 buttons:显示的按钮
#
 default:默认选中的按钮
dlg = SimpleDialog(root,
                   text 
= 'hello SimpleDialog',
                   buttons 
= ['Yes','No','cancel'],
                   default 
= 0,
                   )
# 执行对话框
print dlg.go()
root.mainloop()
# 返回值为点击的按钮在buttons中的索引值
'''2.使用tkSimpleDialog模块'''
# askinteger:输入一个整数值
#
 askfloat:输入一个浮点数
#
 askstring:输入一个字符串
from Tkinter import *
# 引入SimpleDialog模态对话框
from tkSimpleDialog import *

root 
= Tk()
# 输入一个整数,
#
 initialvalue指定一个初始值
#
 prompt提示信息
#
 title提示框标题
print askinteger(title = 'prompt',prompt = 'input a integer:',initialvalue = 100)
# 输入一浮点数
#
 minvalue指定最小值
#
 maxvalue指定最大值,如果不在二者指定范围内则要求重新输入
print askfloat(title = 'float',prompt = 'input a float',minvalue = 0,maxvalue = 11)
# 输入一字符串
print askstring(title = 'string',prompt = 'input a string')
root.mainloop()
# 返回值为各自输入的值。
'''2.打开文件对话框'''
# LoadFileDialog:打开对话框
from Tkinter import *
from FileDialog import *

root 
= Tk()
# 指定master就可以了。
#
 title属性用来指定标题
fd = LoadFileDialog(root)
# go方法的返回值即为选中的文本路径,如果选择取返回值则为None
print fd.go()
root.mainloop()
# 返回选中的文件名称
'''3.保存文件对话框'''
# SaveFileDialog:保存对话框
#
 与LoadFileDialog正好操作相反,这个类是用来保存文件。
#
 各个 参数的意义都  一样,只是ok的返回值为保存的文件名称;如果取消则为None
from Tkinter import *
from FileDialog import *

root 
= Tk()
# 指定master就可以了。
#
 title属性用来指定标题
fd = SaveFileDialog(root)
# go方法的返回值即为选中的文本路径,如果选择取返回值则为None
print fd.go()
root.mainloop()
# 返回选中的文件名称

'''4.使用颜色对话框'''
# askcolor:颜色对话框
from Tkinter import *
# 引入tkColorChoose模块
from tkColorChooser import *
root 
= Tk()

# 调用askcolor返回选中颜色的(R,G,B)颜色值及#RRGGBB表示
print askcolor()
root.mainloop()
# 返回选中的文件名称
'''5. 使用消息对话框'''
# -*- coding: cp936 -*-
#
 showinfo:信息对话框
#
 showwarning:警告对话框
#
 showerror:错误对话框
#
 showquestion:询问对话框  
#
 showokcancel:显示确定/取消对话框  
#
 showyesno:是/否对话框
#
 showretrycancel:重试/取消对话框
#
 使用提示对话框模块tkMessageBox
from Tkinter import *
# 引入tkMessageBox模块
from tkMessageBox import *
root 
= Tk()
stds 
= [
    showinfo,       
# 显示信息消息框
    showwarning,    # 显示警告消息框
    showerror,      # 显示错误消息框
    askquestion,    # 显示询问消息框
    askokcancel,    # 显示确认/取消消息框
    askyesno,       # 显示是/否消息框
    askretrycancel  # 显示重试/取消消息框
    ]
for std in stds:
    
print str(std),std(title = str(std),message = str(std))
# 程序打印输出结果如下(与点击的按钮得到不同其值)
#
 <function showinfo at 0x00D589F0> ok
#
 <function showwarning at 0x00D58A30> ok
#
 <function showerror at 0x00D58A70> ok
#
 <function askquestion at 0x00D58AB0> yes
#
 <function askokcancel at 0x00D58AF0> False
#
 <function askyesno at 0x00D58B30> True
#
 <function askretrycancel at 0x00D58B70> True
root.mainloop()
# 如果要确认点击的是那一个按钮,则可以判断这个消息框的返回值,注意各个值有所不同
#
 返回值有ok/yes/True
'''6.使用缺省焦点'''
# -*- coding: cp936 -*-
#
 default:指定默认焦点位置
#
 使用提示对话框模块tkMessageBox缺省焦点
from Tkinter import *
from tkMessageBox import *
root 
= Tk()
print askokcancel(title = 'quit application?',
                  message 
= 'would you like quit this application',
                  default 
= CANCEL # 指定默认焦点位置
                  )

root.mainloop()
# 使用default来指定默认焦点位置,ABORT/RETRY/IGNORE/OK/CANCEL/YES/NO
#
 如果指定的按钮不存在,在抛出异常
 
原创粉丝点击