Pop-up Window Using Python Source Code
Submitted by razormist on Tuesday, February 18, 2020 - 15:37.
In this tutorial we will create a Pop-up Window using Python. This code will display pop-up window when user click the button. The code use tkinter module to create a layout and widgets that can call python functions. When a function is called it will display a pop-up prompt window that will warn the user. This is a user-friendly kind of program, feel free to modify it and use it to your working program.
We will be using Python because it has a design philosophy which emphasizes code readability. That's why python is very easy to use especially for beginners who just started programming. It is very easy to learn the syntax emphasizes readability and it can reduces time consuming in developing..
There you have it we just created a Pop-up Window Using Python. I hope that this simple tutorial help you for what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!!
Getting started:
First you will have to download & install the Python IDLE's, here's the link for the Integrated Development And Learning Environment for Python https://www.python.org/downloads/.Importing Modules
After setting up the installation and the database, run the IDLE and click file and then new file. After that a new window will appear containing a black file this will be the text editor for the python. Then copy code that I provided below and paste it inside the IDLE text editor.- from tkinter import *
Setting up the Main Frame
After importing the modules, we will now then create the main frame for the application. To do that just copy the code below and paste it inside the IDLE text editor.- root = Tk()
- root.title("Pop-up Window Using Python Source Code")
- width = 400
- height = 150
- screen_width = root.winfo_screenwidth()
- screen_height = root.winfo_screenheight()
- x = (screen_width/2) - (width/2)
- y = (screen_height/2) - (height/2)
- root.geometry("%dx%d+%d+%d" % (width, height, x, y))
- root.resizable(0, 0)
Designing the Layout
After creating the Main Frame we will now add some layout to the application. Just kindly copy the code below and paste it inside the IDLE text editor.- #==============================FRAMES=========================================
- Top = Frame(root, bd=2, relief=RIDGE)
- Top.pack(side=TOP, fill=X)
- Form = Frame(root, height=200)
- Form.pack(side=TOP, pady=20)
- #==============================LABELS=========================================
- lbl_title = Label(Top, text = "Pop-up Window Using Python Source Code", font=('arial', 15))
- lbl_title.pack(fill=X)
- #==============================BUTTON WIDGETS=================================
- btn_login = Button(Form, text="Click me", width=45, command=displayWindow)
- btn_login.grid(pady=25, row=3, columnspan=2)
- btn_login.bind('<Return>', displayWindow)
Creating the Main Function
This is where the code that contains the main functions. This code will display a pop-up window when the button is clicked. To do that just copy and write these blocks of code.- #==============================METHODS========================================
- def displayWindow():
- global Home
- root.withdraw()
- Home = Toplevel()
- Home.title("Python: Simple Login Application")
- width = 800
- height = 200
- screen_width = root.winfo_screenwidth()
- screen_height = root.winfo_screenheight()
- x = (screen_width/2) - (width/2)
- y = (screen_height/2) - (height/2)
- root.resizable(0, 0)
- Home.geometry("%dx%d+%d+%d" % (width, height, x, y))
- lbl_home = Label(Home, text="Welcome! Please visit sourcecodester for more codes!", font=('times new roman', 20)).pack()
- btn_back = Button(Home, text='Back', command=Back).pack(pady=20, fill=X)
- def Back():
- Home.destroy()
- root.deiconify()
Add new comment
- 1613 views