如何从tkinter调用消息框?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何从tkinter调用消息框?相关的知识,希望对你有一定的参考价值。

from tkinter import *

import random

win = Tk()
win.title("Sample")
win.resizable(False,False)
win.configure(bg="#767676")


def game():
    entry = Entry_field.get()
    days = ["Monday","Tuesday","Wednesday","Thursday","Friday","Sunday","Saturday"]
    randomise = random.choice(days)
    messagebox.showinfo("Ответ",randomise)


Label_field = Label(win, text="Choose your day!", font=("outrun",10,"bold"))
Label_field.grid(row=0,column=0)

Notification_Label = Label(win, text="Enter your name here", font=("montserrat",10,"bold"),bg="#EF9A9A")
Notification_Label.grid(row=1 , column=0, sticky=W)

Entry_field = Entry(win, width=30)
Entry_field.grid(row=1, column=1)


Button_field = Button(win, text="Press", command=game)
Button_field.grid(row=1,column=2)



win.mainloop()

[消息框在我从条目中获取数据时看不到请帮助我。我是新手,我想在tkinter中创建示例项目

答案

您正在尝试使用showinfo模块中的tkinter.messagebox功能,但尚未导入。您需要在代码顶部添加import tkinter.messagebox as messageboxfrom tkinter import messagebox行。这是完整的固定代码:

from tkinter import *
import tkinter.messagebox as messagebox
import random


win = Tk()
win.title("Sample")
win.resizable(False, False)
win.configure(bg="#767676")

def game():
    entry = Entry_field.get()
    days = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Sunday", "Saturday"]
    randomise = random.choice(days)
    messagebox.showinfo("Ответ", randomise)

Label_field = Label(win, text="Choose your day!", font=("outrun", 10, "bold"))
Label_field.grid(row=0, column=0)

Notification_Label = Label(win, text="Enter your name here", font=("montserrat", 10, "bold"), bg="#EF9A9A")
Notification_Label.grid(row=1, column=0, sticky=W)

Entry_field = Entry(win, width=30)
Entry_field.grid(row=1, column=1)

Button_field = Button(win, text="Press", command=game)
Button_field.grid(row=1, column=2)

win.mainloop()

以上是关于如何从tkinter调用消息框?的主要内容,如果未能解决你的问题,请参考以下文章

如何创建 tkinter 错误消息框 [重复]

在 Python 3 和 tkinter 中使用变量调用函数

如何在 tkinter 中显示数据框

python tkinter-消息框对话框

如何将腌制数据保存到/从 tkinter 输入框重新加载?

Python中tkinter包如何提取文本框文字到函数之外?