2

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.

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

Sunny Patel
  • 7,830
  • 2
  • 31
  • 46
CogentD
  • 21
  • 1
  • 3
  • I should note that im working on a raspberry pi 3+, operating system Raspbian – CogentD Jun 10 '19 at 17:45
  • How are you detecting that you "see" a certain error in the shell window? By this do you mean any shell window in general, running any program? In other words, if the user types the wrong IP address running a ping command you want to see a popup? Or is it more specific to your programs only? – Mike from PSG Jun 10 '19 at 23:37

3 Answers3

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 !

Urvi Soni
  • 314
  • 1
  • 2
  • 12
0

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

painor
  • 1,119
  • 1
  • 8
  • 25
  • i have tried this but I need the message box to pop up only when I get an error in the shell window – CogentD Jun 10 '19 at 17:57
0

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()
  • I think i see what your doing here. I will give that a try. Basically your saying if the function doesnt run have the messagebox appear? – CogentD Jun 10 '19 at 17:59
  • Not exactly. Validate the ip address or catch an exception. In layman's terms, if the validate function returns false, display the popup. Or, catch the socket error and display the popup. – Paul Blackburn Jun 10 '19 at 18:10
  • I would like this to work with other things then just scoket errors. will this example still apply? – CogentD Jun 10 '19 at 18:19
  • I raised a generic exception as an example. In practice, raise the most specific exception. See https://stackoverflow.com/questions/2052390/manually-raising-throwing-an-exception-in-python – Paul Blackburn Jun 10 '19 at 18:30