python tkinter 使用(二)

python tkinter 使用(二)

本篇文章着重讲下tkinter中messagebox的使用。

1:提示框

def showinfo(event):
    messagebox.showinfo("这是个提示框","this is message content")

2:错误提示框

def showerror(event):
    messagebox.showerror("这是个错误提示框","this is message content")

3:警告提示框

def showwarning(event):
    messagebox.showwarning("这是个警告提示框","this is message content")

4:询问对话框

使用messagebox.askquestion方法 ,该方法的返回值是yes/no.

def askquestion(event):
    result = messagebox.askquestion("请听题:", "中国历史上第一个大一统的国家是秦朝吗?")
    print(result)
    if result == 'yes':
        print("回答正确")
    else:
        print("回答错误")

5:是/否对话框

askyesno的返回值是True/False

def askyesno(event):
    result = messagebox.askyesno("选择吧","这是个弹框吗?")
    print(result)
    if result:
        print("回答正确")
    else:
        print("回答错误")

6:重试取消对话框

askretrycancel 返回值是True/False

def askretrycancel(event):
    result = messagebox.askretrycancel("重试","确认选择?")
    print(result)

7:确认取消对话框

返回值是True/False

def askokcancel(event):
    result = messagebox.askokcancel("取消框","确认取消吗?")
    print(result)

8:是/否/取消 对话框

返回值是True/False/None

def askyesnocancel(event):
    result = messagebox.askyesnocancel("!!!","选择吧!")
    print(result)

最后附上代码:

#!/usr/bin/python3# -*- coding: UTF-8 -*-""" @Author: zh @Time 2023/11/21 下午8:08  . @Email: xxxx @Describe:"""import tkinter as tkfrom tkinter import messagebox# 创建窗口root = tk.Tk()root.title("root")root.geometry("500x500")def showinfo(event):    messagebox.showinfo("这是个提示框","this is message content")def showerror(event):    messagebox.showerror("这是个错误提示框","this is message content")def showwarning(event):    messagebox.showwarning("这是个警告提示框","this is message content")def askquestion(event):    result = messagebox.askquestion("请听题:", "中国历史上第一个大一统的国家是秦朝吗?")    print(result)    if result == 'yes':        print("回答正确")    else:        print("回答错误")def askyesno(event):    result = messagebox.askyesno("选择吧","这是个弹框吗?")    print(result)    if result:        print("回答正确")    else:        print("回答错误")def askretrycancel(event):    result = messagebox.askretrycancel("重试","确认选择?")    print(result)def askokcancel(event):    result = messagebox.askokcancel("取消框","确认取消吗?")    print(result)def askyesnocancel(event):    result = messagebox.askyesnocancel("!!!","选择吧!")    print(result)button = tk.Button(text= "提示框")button.pack()button.bind('<1>', showinfo)button = tk.Button(text= "错误提示框")button.pack()button.bind('<1>', showerror)button = tk.Button(text="警告提示框")button.pack()button.bind('<1>', showwarning)button = tk.Button(text="提问框")button.pack()button.bind('<1>', askquestion)button = tk.Button(text="确认弹框")button.pack()button.bind('<1>', askyesno)button = tk.Button(text="取消框")button.pack()button.bind('<1>', askokcancel)button = tk.Button(text="retry/取消框")button.pack()button.bind('<1>', askretrycancel)button = tk.Button(text="yes/no/cancel 框")button.pack()button.bind('<1>', askyesnocancel)root.mainloop()
posted @ 2023-12-26 20:18  夏沫琅琊  阅读(25)  评论(0编辑  收藏  举报