Here we will create our first window on tkinter.
First, we will import the Tkinter package and create a window and define its title:
We will start by creating a new python file, window.py, save it with the extension .py and enter the following code.
from tkinter import *
window = Tk ()
window.title ("Hello World")
window.mainloop ()
from tkinter import * - here we are importing the tkinter library
window = Tk () - Here we create an instance of the tkinter library
window.title ("Hello World") - The title is an attribute used to define the window title.
window.mainloop () - This function calls the window's infinite loop, so the window will wait for any user interaction until we close it. If you forget to call the mainloop function, nothing will appear for the user.
Python tkinter - Setting the window size
We can set the default window size using the geometry function as window.geometry ('500x500') .
The line above sets the window width to 500 pixels and the height to 500 pixels.
Python Tkinter - Changing background color
We can also change the background color of a Tkinter window, to any color of our choice by using the attribute bg .
from tkinter import *
window = Tk ()
window.title ("Hello World")
window.geometry ('250x250')
window.config(bg='blue')
window.mainloop ()
Post a Comment
0Comments