Python tKinter 2

Create your first GUI application

GUI elements and their functionality are defined in the Tkinter module. The following code demonstrates the steps in creating a UI.

First, we will import Tkinter package and create a window and set its title:

from tkinter import *
window=Tk()
# add widgets here

window.title('Hello Python')
window.mainloop()

* First of all, import the TKinter module.

* After importing, setup the application object by calling the Tk() function. This will create a top-level window (root) having a frame with a title bar, control box with the minimize and close buttons, and a client area to hold other widgets.

* The application object then enters an event listening loop by calling the mainloop() method. The application is now constantly waiting for any event generated on the elements in it. The event could be text entered in a text field, a selection made from the dropdown or radio button, single/double click actions of mouse, etc. The application’s functionality involves executing appropriate callback functions in response to a particular type of event. We shall discuss event handling later in this tutorial. The event loop will terminate as and when the close button on the title bar is clicked.

The above code will create the following window:

Output:

from tkinter import *
window=Tk()
# add widgets here

window.title('Hello Python')
window.geometry("300x200+10+20")
window.mainloop()

* First of all, import the TKinter module.
* After importing, setup the application object by calling the Tk() function. This will create a top-level window (root) having a frame with a title bar, control box with the minimize and close buttons, and a client area to hold other widgets.

* The geometry() method defines the width, height and coordinates of the top left corner of the frame as below (all values are in pixels): window.geometry(“widthxheight+XPOS+YPOS”)

* The application object then enters an event listening loop by calling the mainloop() method. The application is now constantly waiting for any event generated on the elements in it. The event could be text entered in a text field, a selection made from the dropdown or radio button, single/double click actions of mouse, etc. The application’s functionality involves executing appropriate callback functions in response to a particular type of event. We shall discuss event handling later in this tutorial. The event loop will terminate as and when the close button on the title bar is clicked.
The above code will create the following window:

 

to change the background color of window

from tkinter import *
window=Tk()
# add widgets here

window.title('Hello Python')
window.geometry("300x200+10+20")
#to change the background color of window
window.configure(bg='blue')
window.mainloop()

Output:

Visitor Counter

0051040