gpt4 book ai didi

python - 如何从 tkinter 调用消息框?

转载 作者:行者123 更新时间:2023-12-01 06:22:14 24 4
gpt4 key购买 nike

我正在 Python 中创建一个基于 Tkinter 的 GUI,但遇到一个问题:当我从 Entry 小部件获取数据时,消息框不会出现。怎么解决呢?

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.messagebox 模块中的 showinfo 函数,但尚未导入它。您需要将 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()

关于python - 如何从 tkinter 调用消息框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60308215/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com