Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Messagebox
#1
Why can't I see the messagebox when input is not a, b, or c? I am sure there is a good reason but I guess I can't find out yet.

Thanks.

from tkinter import*
from tkinter import messagebox

def math():

    while True:
        
        data=input("enter a,b or c ")

        if data=='a':
            data=5
        elif data=='b':
            data=10
        elif data=='c':
            data=15
        else :
            
            hi=messagebox.showerror(title="Try",message="Wrong Input ")
            data=0
        print(data)


math()
Reply
#2
Because you are not running tkinter. You need to call Tk() mainloop().
Reply
#3
It's bad practice to do wildcard import e.g.(from tkinter import *)
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#4
You don't need to assign messagebox to a variable. Just call messagebox.showerror straight away.

As @menator01 said, it's bad practice to import everything from a module. In this case, you don't even need to import anything else than messagebox.

Also, don't forget to break from the while loop or you will be stuck in it forever.

from tkinter import messagebox

def math():
    while True:
        data = input("enter a,b or c ")
        if data == 'a':
            data=5
        elif data == 'b':
            data=10
        elif data == 'c':
            data=15
        else:
            messagebox.showerror(title="Try",message="Wrong Input")
            data=0
        print(data)
        break

if __name__ == "__main__":
    math()
Reply
#5
Here is an example of your code modified

from tkinter import messagebox

def func():
    while True:
        data = input('enter a, b, or c. To quit enter q\n>> ')
        if data == 'a':
            data = 5
        elif data == 'b':
            data = 10
        elif data == 'c':
            data = 15
        elif data == 'q':
            print('Goodbye!')
            break
        else:
            messagebox.showerror(title='Error', message='Wrong Input')
            data = 0
        print(f'data was set to {data}\n')
    
func()
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#6
@carecavoador
Sorry, just realized my code is the same as yours.
carecavoador likes this post
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#7
You cannot create a message box without first calling Tk(). mainloop() is not required. I just ran a test to verify. You can unmap the window created by Tk().
import tkinter as tk
from tkinter import messagebox

def func():
    while True:
        data = input('enter a, b, or c. To quit enter q\n>> ')
        if data == 'a':
            data = 5
        elif data == 'b':
            data = 10
        elif data == 'c':
            data = 15
        elif data == 'q':
            print('Goodbye!')
            break
        else:
            messagebox.showerror(title='Error', message='Wrong Input')
            data = 0
        print(f'data was set to {data}\n')
     
root = tk.Tk()
root.withdraw()
func()
Reply
#8
I removed the tkinter import and didn't call tk. Just used the from tkinter import messagebox. Worked in vs studio and terminal. On Ubuntu. My first attempt was the setup you have for the code calling tk.
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#9
Interesting. Doesn't work for me on Windows using either vscode or powershell. A message box pops up, but so does an empty tkinter window.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Tkinter messagebox WiPi 4 2,103 Dec-10-2022, 09:19 AM
Last Post: WiPi
  'No module named tkinter.messagebox' - PyInstaller ironfelix717 7 8,271 Jan-19-2020, 06:56 AM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020