How to create window in Tkinter Python GUI



Tkinter GUI window creation 

  • Import Tkinter/tkinter module

To create a simple window, the very 1st step to import Tkinter module

  • Python 2 supports Tkinter module
  • Python 3 supports tkinter module.
so with the help of exception handling we can make our application to run on both i.e. Pyhton_2 and Python_3 as below

#program

try:
import Tkinter as tk        # to import in Python_2
except
import tkinter as tk        # to import in Python_3

If software is python_3, import Tkinter will throw exception which is handled in except part by importing tkinter. 


  • How to create simple window with Tkinter GUI
After importing tkinter, to create window need to call Tk(), main GUI interface.
Tk() will return tkinter.Tk class object to the created window.
tkinter.Tk class object will be used to interact with the window for all operations related to placing widgets and raise events.

mw is our the tkinter.Tk class object for these tutorials and all syntax and examples will be explained by mw


#program

try:
import Tkinter as tk
except:
import tkinter as tk

mw = tk.Tk()
print("type(mw) : ", type(mw))
mw.mainloop()


#Output

type(mw) : <class 'tkinter.Tk'>

GUI Output:


by executing above code, Tk() will create a window with default size and default position on screen(left top corner)


  • How to set window dimension

geometry('width x height + x-axis + y-axis')

width: width of window

height: height of window

x-axis: window placement, how much shift on x-axis(how much right from left side of screen)

y-axis: how much shift on y-axis(how much down from top)


#program

try:
import Tkinter as tk
except:
import tkinter as tk

mw = tk.Tk()
mw.geometry(
'300x300+200+50')    #adding dimension to windows with position on screen
mw.mainloop()

#


GUI Output:

window is 200 shift on x-axis and 60 shift ob y axis with a dimension of 300x300


  • How to set window name

title("name of the window")


#program

try:
import Tkinter as tk
except:
import tkinter as tk

mw = tk.Tk()
mw.geometry(
'500x300+10+10')
mw.title(
"AETies : Automation Expert Technologies")    # adding title to window
mw.mainloop()

GUI Output:


  • How to remove default top end bar from your window.

overridedirect()

by default it is False


mw.overrideredirect(False) # will show top bar of window, hiding the default close resize and minimize option

mw.overrideredirect(True) # will remove top bar of window, hiding the default close resize and minimize option

 
#program

try :
from Tkinter import * # supports Python 2
except:
from tkinter import * # supports Python 3

mw = Tk()
mw.title(
"AETies : Automation Expert Technologies")
mw.overrideredirect(
1)
mw.geometry(
'550x150+10+50')

mw.mainloop()


GUI Output:



  • How to set image in window background.

we need to create a photo object and then need to create a LABEL (Tkinter widget and then need to set image there)

Image(image_type, Image_name)


#program

try:
from Tkinter import *
except:
from tkinter import *

mw = Tk()
mw.geometry(
'500x300+10+10')
mw.title(
"AETies : Automation Expert Technologies")

photo = Image(
"photo", file="logo.gif")
mw_label = Label(mw
, image=photo)
mw_label.pack(
fill = BOTH, expand=YES)

mw.mainloop()

GUI Output:


  • How to set favicon in window.


Above highlighted icon is the default ICON(favicon) in windows and Linux, MAC does not support icon change

iconphoto()

we need to create a photo object and then need to create a LABEL (Tkinter widget and then need to set image there)



#program

try :
from Tkinter import * # supports Python 2
except:
from tkinter import * # supports Python 3

mw = Tk()
mw.title("AETies : Automation Expert Technologies")
mw.overrideredirect(0)
mw.geometry('550x150+10+50')
photo = PhotoImage(file="favicon.png")
mw.iconphoto(False, photo)

mw.mainloop()


GUI Output:






Comments

My photo
Techno Xpresss
Bangalore, Karnataka, India