How to Change the position of MessageBox using Python Tkinter

PythonTkinterGUI-Programming

Let us suppose that we want to create a dialogue box using tkinter. To create the dialogue box we can use the MessageBox library which contains several functions to quickly create dialogue types.

To adjust the position of the created Dialogue Box, we can use its “toplevel” property which basically gives the priority to the current box and keeps all the other processes in the backend.

It contains some other functions like title, message, and details. To change the position of the MessageBox widget, we will use geometry method.

Example

#import the tkinter library

from tkinter import *

#define the messagebox function
def messagebox():

#toplevel function creates MessageBox dialog which appears on top of the screen
   top=Toplevel(win)
   top.title("Click Me")
   #Define the position of the MessageBox
   x_position = 600
   y_position = 400
   top.geometry(f"600x200+{x_position}+{y_position}")
   #Define the property of the messageBox
   l1=Label(top, text= "Hello! TutorialsPoint",bg= "green", fg=
"white",font=('Times New Roman', 24),height=50, width= 50).pack()

#Create an instance of the tkinter frame
#And resize the frame
win = Tk()
win.geometry("600x200")
win.title("Window-1")
Button(win, text="Click Me", command=messagebox,
width=8).pack(pady=80)

win.mainloop()

Output

Running the above code will generate the following output window.

When you click the “Click Me” button, it will open the following dialogue box which can be positioned later.

raja
Published on 04-Mar-2021 13:58:02
Advertisements