How to create a tkinter error message box [duplicate] - python

This question already has answers here:
How to create a message box with tkinter?
(5 answers)
Closed 5 years ago.
I am wanting to create a simple message box in Tkinter that appears and displays the exact error message. Could anyone direct me to how this might be
achieved in tkinter, I have not been able to find much on this topic.
E.g:
traceback.format_exc().replace(':', '-')
ctypes.windll.user32.MessageBoxW(0, "Error", "Have you checked your fridge?"d, 1)
^
#'SyntaxError: invalid syntax'
I am wanting to add this with pyinstaller. I suppose pyinstaller creates a text file and you can see in cmd before it closes, but it would be nice if a message box appear with exact traceerror.

from tkinter import messagebox
messagebox.showerror("Title", "Message")
check here for more info

This login system which will pop up messagebox when you provide wrong data for entry messagebox should be entered into the entry if not the messagebox will pop up prompting you an error as occured
from tkinter import *
from tkinter import messagebox
def top():
if entry1.get() == "messagebox":
log.destroy()
root.deiconify()
else:
messagebox.showerror("error", "try again")
messagebox.showinfo("my message","this is an example of showinfo\nmessagebox")
messagebox.showwarning("warning", "show warning example in tkinter" )
root = Tk()
root.geometry("400x400")
log = Toplevel(root)
log.geometry("200x200")
label1 = Label(log, text="password")
entry1 = Entry(log)
button1 = Button(log, text="login", command=top)
label1.pack()
entry1.pack()
button1.pack(side="bottom")
lab = Label(root, text="welcome bro").pack()
root.withdraw()
root.mainloop()

Related

Tkinter only prints empty strings

I am trying to create a GUI with tkinter to print a string entered in the entry box. I have written the following code
import tkinter as tk
top= tk.Tk()
labelAmount=tk.Label(text='Amount')
Amount= tk.Entry()
amount=Amount.get()
Add = tk.Button(top, text='Add', command = lambda : print(amount))
Add.pack()
Amount.pack()
labelAmount.pack()
top.mainloop()
The problem is that when I am pressing the 'Add' button, an empty string is being printed.
It would be great if someone can help me with this problem.

Why my button in tkinter don't works normal [duplicate]

This question already has answers here:
Why is my Button's command executed immediately when I create the Button, and not when I click it? [duplicate]
(5 answers)
Closed last year.
I wanted to make button in tkinter, but when I started program, the command always calls when code just starts.
Here is example code:
import tkinter as tk
from tkinter import messagebox
window = tk.Tk()
window.title("Why this don't works???")
window.wm_geometry("100x100")
def message():
messagebox.showinfo("Hi there")
button = tk.Button(text="Hello", command=message())
button.grid(column=0, row=0)
while True:
window.update()
And then, button didn't worked. (When you press it, it don't works.)
I don't know what I'm doing wrong, so I need help.
The command should be a pointer to a function
In the code you wrote, the command gets the return value from the function.
command=message()
The correct way is
command = message
The problem is you are requesting a return value from the fucnction. Try using this.
from tkinter import *
# import messagebox from tkinter module
import tkinter.messagebox
# create a tkinter root window
root = tkinter.Tk()
# root window title and dimension
root.title("When you press a button the message will pop up")
root.geometry('75x50')
# Create a messagebox showinfo
def onClick():
tkinter.messagebox.showinfo("Hello World!.", "Hi I'm your message")
# Create a Button
button = Button(root, text="Click Me", command=onClick, height=5, width=10)
# Set the position of button on the top of window.
button.pack(side='top')
root.mainloop()
You have 2 errors:
first:
It must be command=message
second:
You must give a message argument too, you entered a title only.
Or, what you can do is.
Add another variable.
command = message()
Before this line,
button = tk.Button(text="Hello", command=message())
And chande this line to,
button = tk.Button(text="Hello", command=command)

how to handle the "delete window" protocol on messageboxes- Python tkinter

Let's say I have a message box in Python tkinter, for example:
from tkinter import messagebox
messagebox.showinfo("Example", "This is an example")
I want to be able to handle the message box's closing protocol. I know you can do it with tkinter windows, like so:
window.protocol("WM_DELETE_WINDOW", on_closing)
But my question is how do you do it with message boxes?
I figured out that what I wanted to do isn't possible (with the help of #Matiiss). So what I did instead was I created a toplevel widget, added buttons to it and handled its closing protocol. Example:
import tkinter as tk
from tkinter import messagebox
window = tk.Tk()
window.title("Example")
window.state("zoomed")
def protocol_handler():
# code
window2 = tk.Toplevel(window)
window2.geometry("300x220+500+200")
label1 = tk.Label(window2, text="Would you like to continue?")
label1.place(x=20, y=10)
button1 = tk.Button(window2, text="Yes")
button1.place(x=50, y=100)
button2 = tk.Button(window2, text="No")
button1.place(x=100, y=100)
window2.protocol("WM_DELETE_WINDOW", protocol_handler)
window.mainloop()

Printed text should appear in separate window - How?

this question will be relatively simple, just do not get anywhere. It's more of a basic question.
When I write
print ("hello world")
it appears in the script program in the message box bellow. But now I want it to open in a separate fesnter, created with tkinter. I wrote it down like this (see picture or code), but I know that print itself must not be in brackets, how can I solve this problem?
from tkinter import *
a = ("Hello World")
root = Tk()
T = Text(root, height=50, width=150)
T.pack() T.insert(END, print a)
mainloop()
The "hello world" must appear in a separately-opened window.
I am happy about any answer.
Thanks in advance
PyBeginner
I didn't understand your question but can you try this one if it helps .
from tkinter import *
root = Tk()
a = Label(root, text ="Hello World")
a.pack()
root.mainloop()
Resource: https://www.geeksforgeeks.org/python-tkinter-messagebox-widget/ Also, if you want to you can delete showinfo code and just run the w.
from tkinter import *
from tkinter import messagebox
root = Tk()
root.geometry("400x300")
w = Label(root, text='Hello World!', font="50")
w.pack()
messagebox.showinfo("Hello World", "This is a Hello World message!!!")
root.mainloop()

Show user a message in python

quick question here. Is there a really easy way to show a user a message in like a text box in python? I just need to input a string beforehand so I can show the message after a loop runs. Thanks!
EDIT: Just Windows 8.1 and straight python 2.7
If I'm correct that you want a window to let a user type in some text, then show it back as a message box, then you need two modules, tkMessageBox and Tinker, two standard modules in Python 2.7+.
The following documented code does the above
from Tkinter import *
import tkMessageBox
def getInfo():
#This function gets the entry of the text area and shows it to the user in a message box
tkMessageBox.showinfo("User Input", E1.get())
def quit():
#This function closes the root window
root.destroy()
root = Tk() #specify the root window
label1 = Label( root, text="User Input") #specify the label to show the user what the text area is about
E1 = Entry(root, bd =5) #specify the input text area
submit = Button(root, text ="Submit", command = getInfo) #specify the "submit" button that runs "getInfo" when clicked
quit_button = Button(root, text = 'Quit', command=quit) #specify the quit button that quits the main window when clicked
#Add the items we specified to the window
label1.pack()
E1.pack()
submit.pack(side =BOTTOM)
quit_button.pack(side =BOTTOM)
root.mainloop() #Run the "loop" that shows the windows

Categories