Skip to content

How to Display python script Error in Tkinter message box

I have written the below code for run a python file on button click through tkinter and also displaying two button for run python file on GUI window. Now my pyhton files is working fine when click on button. But there is one issue is I want to shows error on tkinter error box whatever error shows on cmd window. If I run python scripts in cmd it shows error. But want to show on tkinter error message box. How is it possible. Below I am sharing you code.

import sys
import os
from tkinter import *
from tkinter import messagebox
from tkinter import ttk

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

# Set the size of the tkinter window

# Define a function to show the popup message
def show_msg():
   messagebox.showinfo("Message","Email Sent Successfully.")

def gen_certificate():
   messagebox.showinfo("Message","Certificate Generated Successfully.")

# Add an optional Label widget
Label(win, text= "Welcome to MBR Admin!", font= ('Aerial 17 bold italic')).pack(pady= 30)

# Create a Button to display the message
ttk.Button(win, text= "Send Mail", command=show_msg).pack(pady= 20)
ttk.Button(win, text= "Generate Certificate", command=gen_certificate).pack(pady= 20)

enter image description here

I would be appreciate if anyone response my questions. Thank you


A way is to use traceback module from python to get more info on the error. But in your example it wont be much useful because os.system returns 1 or 0 depending on if it failed or not(and does not trigger a traceback) but is a much better option:

import traceback

    messagebox.showinfo("Message","Certificate Generated Successfully.")
except Exception:

Try reading this if you want to use os.system, still