首页 > 脚本专栏 > python > python tkinter消息框

python tkinter的消息框模块(messagebox,simpledialog)

作者:手可摘星辰。

这篇文章主要介绍了python tkinter的消息框模块,帮助大家更好的理解和使用python,感兴趣的朋友可以了解下

tkinter提供了三个模块,可以创建弹出对话窗口: (使用必须单独导入模块)

1.messagebox  消息对话框

示例:askokcancel

import tkinter # 导入消息对话框子模块 import tkinter.messagebox # 创建主窗口 root = tkinter.Tk() # 设置窗口大小 root.minsize(300,300) # 声明函数 def okqqq(): # 弹出对话框 result = tkinter.messagebox.askokcancel(title = '标题~',message='内容:要吃饭嘛?')  # 返回值为True或者False print(result) # 添加按钮 btn1 = tkinter.Button(root,text = 'ok',command = okqqq) btn1.pack() # 加入消息循环 root.mainloop()

示例:askquestion

import tkinter # 导入消息对话框子模块 import tkinter.messagebox # 创建主窗口 root = tkinter.Tk() # 设置窗口大小 root.minsize(300,300) # 声明函数 def question(): # 弹出对话框 result = tkinter.messagebox.askquestion(title = '标题',message='内容:你吃饭了嘛?') # 返回值为:yes/no print(result) # 添加按钮 btn1 = tkinter.Button(root,text = 'question',command = question) btn1.pack() # 加入消息循环 root.mainloop()

示例:askretrycancel  (重试)

import tkinter # 导入消息对话框子模块 import tkinter.messagebox # 创建主窗口 root = tkinter.Tk() # 设置窗口大小 root.minsize(300,300) # 声明函数 def retry(): # 弹出对话框 result = tkinter.messagebox.askretrycancel(title = '标题',message='内容:女生拒绝了你!?') # 返回值为:True或者False print(result) # 添加按钮 btn1 = tkinter.Button(root,text = 'retry',command = retry) btn1.pack() # 加入消息循环 root.mainloop()

示例:askyesno

# 声明函数 def yesno(): # 弹出对话框 result = tkinter.messagebox.askyesno(title = '标题',message='内容:你喜欢我吗?') # 返回值为:True或者False print(result) # 添加按钮 btn1 = tkinter.Button(root,text = 'yesno',command = yesno) btn1.pack()

示例:showerror (出错)

# 声明函数 def error(): # 弹出对话框 result = tkinter.messagebox.showerror(title = '出错了!',message='内容:你的年龄不符合要求。') # 返回值为:ok print(result) # 添加按钮 btn1 = tkinter.Button(root,text = 'error',command = error) btn1.pack()

示例:showwarning(警告)

# 声明函数 def warning(): # 弹出对话框 result = tkinter.messagebox.showwarning(title = '出错了!',message='内容:十八岁以下禁止进入。') # 返回值为:ok print(result) # 添加按钮 btn1 = tkinter.Button(root,text = 'warning',command = warning) btn1.pack()

示例:showinto (信息提示)

# 声明函数 def info(): # 弹出对话框 result = tkinter.messagebox.showinfo(title = '信息提示!',message='内容:您的女朋友收到一只不明来历的口红!') # 返回值为:ok print(result) # 添加按钮 btn1 = tkinter.Button(root,text = 'info',command = info) btn1.pack()

2.simpledialog  简单信息对话框

示例:asksting(获取字符串)

import tkinter # 导入子模块 import tkinter.simpledialog # 创建主窗口 root = tkinter.Tk() # 设置窗口大小 root.minsize(300,300) # 创建函数 def askname(): # 获取字符串(标题,提示,初始值) result = tkinter.simpledialog.askstring(title = '获取信息',prompt='请输入姓名:',initialvalue = '可以设置初始值') # 打印内容 print(result) # 添加按钮 btn = tkinter.Button(root,text = '获取用户名',command = askname) btn.pack() # 加入消息循环 root.mainloop()

示例:askinteger(获取整型)

import tkinter # 导入消息对话框子模块 import tkinter.simpledialog # 创建主窗口 root = tkinter.Tk() # 设置窗口大小 root.minsize(300,300) # 创建函数 def askage(): # 获取整型(标题,提示,初始值) result = tkinter.simpledialog.askinteger(title = '获取信息',prompt='请输入年龄:',initialvalue = '18') # 打印内容 print(result) # 添加按钮 btn = tkinter.Button(root,text = '获取年龄',command = askage) btn.pack() # 加入消息循环 root.mainloop()

示例:askfloat(获取浮点型)

import tkinter # 导入消息对话框子模块 import tkinter.simpledialog # 创建主窗口 root = tkinter.Tk() # 设置窗口大小 root.minsize(300,300) # 创建函数 def askheight(): # 获取浮点型数据(标题,提示,初始值) result = tkinter.simpledialog.askfloat(title = '获取信息',prompt='请输入身高(单位:米):',initialvalue = '18.0') # 打印内容 print(result) # 添加按钮 btn = tkinter.Button(root,text = '获取身高',command = askheight) btn.pack() # 加入消息循环 root.mainloop()

以上就是python tkinter的消息框模块的详细内容,更多关于python tkinter消息框的资料请关注脚本之家其它相关文章!

您可能感兴趣的文章:
  • python3 最常用的三种装饰器语法汇总
    python3 最常用的三种装饰器语法汇总
    2022-06-06
  • python程序的打包分发示例详解
    python程序的打包分发示例详解
    2022-06-06
  • 利用Python爬虫爬取金融期货数据的案例分析
    利用Python爬虫爬取金融期货数据的案例分析
    2022-06-06
  • python数字图像处理skimage读取显示与保存图片
    python数字图像处理skimage读取显示与保存图片
    2022-06-06
  • python使用tkinter模块实现文件选择功能
    python使用tkinter模块实现文件选择功能
    2022-06-06
  • python memory_profiler库生成器和迭代器内存占用的时间分析
    python memory_profiler库生成器和迭代器内存占用的时间
    2022-06-06
  • python数字图像处理环境安装与配置过程示例
    python数字图像处理环境安装与配置过程示例
    2022-06-06
  • 美国设下计谋,用娘炮文化重塑日本,已影响至中国
    美国设下计谋,用娘炮文化重塑日本,已影响至中国
    2021-11-19
  • 时空伴随者是什么意思?时空伴随者介绍
    时空伴随者是什么意思?时空伴随者介绍
    2021-11-09
  • 工信部称网盘企业免费用户最低速率应满足基本下载需求,天翼云盘回应:坚决支持,始终
    工信部称网盘企业免费用户最低速率应满足基本下载需求,天翼云盘回应:坚决支持,始终
    2021-11-05
  • 2022年放假安排出炉:五一连休5天 2022年所有节日一览表
    2022年放假安排出炉:五一连休5天 2022年所有节日一览表
    2021-10-26
  • 电脑版 - 返回首页

    2006-2023 脚本之家 JB51.Net , All Rights Reserved.
    苏ICP备14036222号