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

 更新时间:2020年11月07日 15:48:19   作者:手可摘星辰。  
这篇文章主要介绍了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消息框的资料请关注脚本之家其它相关文章!

相关文章

  • python 实现图与图之间的间距调整subplots_adjust

    python 实现图与图之间的间距调整subplots_adjust

    这篇文章主要介绍了python 实现图与图之间的间距调整subplots_adjust,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-05-05
  • Python进阶学习之带你探寻Python类的鼻祖-元类

    Python进阶学习之带你探寻Python类的鼻祖-元类

    这篇文章主要介绍了Python进阶学习之带你探寻Python类的鼻祖-元类,文中有非常详细的解释,对正在学习python的小伙伴们有很好的帮助,需要的朋友可以参考下
    2021-05-05
  • numpy.linspace函数具体使用详解

    numpy.linspace函数具体使用详解

    这篇文章主要介绍了numpy.linspace具体使用详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-05-05
  • 详解python安装matplotlib库三种失败情况

    详解python安装matplotlib库三种失败情况

    这篇文章主要介绍了详解python安装matplotlib库三种失败情况,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-07-07
  • Python寻找路径和查找文件路径的示例

    Python寻找路径和查找文件路径的示例

    今天小编就为大家分享一篇Python寻找路径和查找文件路径的示例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2019-07-07
  • Tensor 和 NumPy 相互转换的实现

    Tensor 和 NumPy 相互转换的实现

    本文主要介绍了Tensor 和 NumPy 相互转换的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-02-02
  • typing.Dict和Dict的区别及它们在Python中的用途小结

    typing.Dict和Dict的区别及它们在Python中的用途小结

    当在 Python 函数中声明一个 dictionary 作为参数时,我们一般会把 key 和 value 的数据类型声明为全局变量,而不是局部变量。,这篇文章主要介绍了typing.Dict和Dict的区别及它们在Python中的用途小结,需要的朋友可以参考下
    2023-06-06
  • 一文读懂Python的’=='和’is’用法

    一文读懂Python的’=='和’is’用法

    探索Python世界的'=='与'is',一个看似简单却隐藏玄机的话题,本指南将带你轻松读懂它们背后的故事,解锁编程中的这道难题,开始我们的快速之旅,释放代码的潜能吧!
    2024-01-01
  • python获取array中指定元素的示例

    python获取array中指定元素的示例

    今天小编就为大家分享一篇python获取array中指定元素的示例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2019-11-11
  • pandas dataframe 中的explode函数用法详解

    pandas dataframe 中的explode函数用法详解

    这篇文章主要介绍了pandas dataframe 中的explode函数用法详解,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-05-05

最新评论