First, let's make shure that our tkinter module is working ok with simple for loop that will spawn 5 instances of blank Tk window.
We will import all options from tkinter module because we are using only one module.
If using multiple modules, if we have some big scripts, we can use only specific methods from those modules. With that approach we are evading possible name clashes:
from tkinter import *
for x in range(5):
Tk()
After using 3 lines from above, if you see all 5 windows on screen, all is ok with import.
Then we can proceed with import of messagebox, alias mb:
import tkinter.messagebox as mb
We need to initialize a blank window with this line:
top_win = Tk()
Then, geometry of a window is set o be 500 x 500:
top_win.geometry("500x500")
Function do_something() is needed to show msg box. Inside msg box, we will have mb activated targeting showinfo() method to show "lorem ipsum stuff" as Title in a window:
def do_something():
mb.showinfo("Title", "lorem ipsum stuff")
It's not enough just to have our custom function, we need to activate it on button. A name for button will be but_1.
That button is located in top_win, and text inside it will be "Yeah". To connect button with function we are using command = do_something, without parenthesis:
but_1 = Button(top_win, text = "Yeah", command = do_something)
Of course, button but_1 must be positioned using coordinates x and y, using method place():
but_1.place(x = 2, y = 2)
Mainloop is needed at the bottom of script:
top_win.mainloop()
This is full script:
from tkinter import *
import tkinter.messagebox as mb
top_win = Tk()
top_win.geometry("500x500")
def do_something():
mb.showinfo("Title", "lorem ipsum stuff")
but_1 = Button(top_win, text = "Yeah", command = do_something)
but_1.place(x = 2, y = 2)
top_win.mainloop()