About Lesson
In this TKinter Tutorial we are going to learn about creating TKinter MessageBox, there are different types of messagebox that you can use in tkinter, so we have Information, Warning and Error MessageBox. this is the complete code for MessageBox.
This is the complete code for this lesson.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
import tkinter as tk from tkinter import ttk from tkinter import messagebox as msg class Window(tk.Tk): def __init__(self): super(Window, self).__init__() self.title("MessageBox") self.minsize(500,400) self.wm_iconbitmap("myicon.ico") self.create_button() def create_button(self): self.btn = ttk.Button(self, text = "Info MessageBox", command = self.info_msg) self.btn.grid(column = 1, row = 1) self.btn1 = ttk.Button(self, text="Error MessageBox", command=self.error_msg) self.btn1.grid(column=2, row=1) self.btn2 = ttk.Button(self, text="Warn MessageBox", command=self.warn_msg) self.btn2.grid(column=3, row=1) def info_msg(self): msg.showinfo("TKinter Info Message Box", "This GUI Is Build In Python TKinter") def warn_msg(self): msg.showwarning("Warning Message Title", "This is a warning") def error_msg(self): msg.showerror("Error Message Title", "This is an error message box") window = Window() window.mainloop() |
First of all you need to import messagebox from tkinter.
1 |
from tkinter import messagebox as msg |
We are going to create three buttons for three messagebox, also we have connected a method to these buttons for opening messageboxes.
1 2 |
self.btn = ttk.Button(self, text = "Info MessageBox", command = self.info_msg) self.btn.grid(column = 1, row = 1) |
And these are three different messageboxes, and we have already connected these with the specific buttons.
1 2 3 4 5 6 7 8 9 10 |
def info_msg(self): msg.showinfo("TKinter Info Message Box", "This GUI Is Build In Python TKinter") def warn_msg(self): msg.showwarning("Warning Message Title", "This is a warning") def error_msg(self): msg.showerror("Error Message Title", "This is an error message box") |
Run the complete code and this is the result.