Hướng dẫn python windows error message

I have a program and if you enter the ip address wrong the shell window will produce a socket error. I want to create a pop up message box that runs anytime I see a certain error in the shell window.

Show

I cant really find a whole lot of info on something like this.

Sunny Patel

7,5542 gold badges31 silver badges42 bronze badges

asked Jun 10, 2019 at 17:33

2

You can use PySimple GUI. It's easy to implement.

For that you need to install it.

You can Install it through this command :-

pip install PySimpleGUI

Code for Invalid Ip address of generating Pop Up is as follow:-

import PySimpleGUI as sg
import socket

try:
    socket.inet_aton('256.0.0.1')
    print("Validate IP")
    # legal
except socket.error:
    # Not legal
    sg.Popup('Opps!', 'Wrong IP Address!')

Hope this Helpful !

answered Jun 10, 2019 at 18:07

Urvi SoniUrvi Soni

3041 gold badge2 silver badges11 bronze badges

1

you could do something like this if you want a popup message box if you are using windows.

import ctypes

ctypes.windll.user32.MessageBoxW(0, u"Error", u"Error", 0)

else you could use Tkinter using

import tkinter
from tkinter import messagebox

# hide main window
root = tkinter.Tk()
root.withdraw()

# message box display
messagebox.showerror("Error", "Error message")
messagebox.showwarning("Warning","Warning message")
messagebox.showinfo("Information","Informative message")

check this tutorial for more

answered Jun 10, 2019 at 17:42

painorpainor

9611 gold badge7 silver badges23 bronze badges

1

Does Tcl/Tk fit your use case?

from tkinter import *

def validate(address):
    valid = False
    # validation logic
    if not valid:
        raise Exception("Socket Error")

if __name__ == "__main__":
    ip = r"https://stackoverflow.com/"
    try:
        validate(ip)
    except Exception as e:
        root = Tk()
        w = Label(root, text=e)
        w.pack()
        root.mainloop()

answered Jun 10, 2019 at 17:52

4


The Tkinter library has many built-in functions and methods which can be used to implement the functional part of an application. We can use messagebox module in Tkinter to create various popup dialog boxes. The messagebox property has different types of built-in popup windows that the users can use in their applications.

If you need to display the error messagebox in your application, you can use showerror("Title", "Error Message") method. This method can be invoked with the messagebox itself.

Example

# Import the required libraries
from tkinter import *
from tkinter import messagebox

# Create an instance of tkinter frame or window
win = Tk()

# Set the size of the tkinter window
win.geometry("700x350")

# Define a function to show the error message
def on_click():
   messagebox.showerror('Python Error', 'Error: This is an Error Message!')

# Create a label widget
label = Label(win, text="Click the button to show the message ",
font=('Calibri 15 bold'))
label.pack(pady=20)


# Create a button to delete the button
b = Button(win, text="Click Me", command=on_click)
b.pack(pady=20)

win.mainloop()

Output

When you run the above code, it will show a button widget and a label in the window. Click the button to show the error message.

Hướng dẫn python windows error message

Updated on 06-Aug-2021 07:40:41

  • Related Questions & Answers
  • How to create a message box with Tkinter?
  • How can I create a simple message box in Tkinter?
  • Create an alert message box with Bootstrap
  • Creating a popup message box with an Entry field in tkinter
  • How to create a combo box with auto-completion in Tkinter?
  • How to change MySQL error message language?
  • JavaScript Error message Property
  • How to center the JavaScript alert message box?
  • How to stop Tkinter Message widget from resizing?
  • How to create a chat message with CSS?
  • comp_err - Compile MySQL Error Message File
  • How do I print a message to the error console using JavaScript?
  • How to set justification on Tkinter Text box?
  • How to create a Box Layout in Java?
  • How to create a flip box with CSS?