3

I'm creating a Tkinter-based GUI in Python and I have a problem: messagebox does not appear when I'm getting data from the Entry widget. How can I solve it?

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()
2
  • 2
    You need to put this line at the top: import tkinter.messagebox as messagebox Commented Feb 19, 2020 at 19:59
  • 1
    Put this at the top of your code and everything will work fine: from tkinter import messagebox. Commented Feb 20, 2020 at 8:28

1 Answer 1

2

You are trying to use the showinfo function from the tkinter.messagebox module, but haven't imported it. You need to add import tkinter.messagebox as messagebox or from tkinter import messagebox line to the top of your code. Here is the full fixed code:

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()
Sign up to request clarification or add additional context in comments.

1 Comment

I think using from tkinter import messagebox is a better way.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.