GUI Module (Tkinter)-
- – It is used to create windows-based GUI application in Python.
- – tkinter module contains many classes which helps us to develop windows-based application.
- – Object creation of class Tk provides the result of window.
Ex-
import tkinter
window = tkinter.Tk()
window.mainloop() # This helps us to display a small window.
Layout-
- – Arrangement of the component on the window.
- – Few of the component are lable, buttons, entry
- – Every component has 3 object level methods which are used to layout the window and these are-
- – pack(self), place(self), grid(self)
Every Component Class Is Having 3 Object Level Methods-
class component:
def pack(self):
-----
-----
def place(self): #will be used mostly
-----
-----
def grid(self):
-----
-----
component.pack()
component.place()
component.grid()
- We always need to pass the object reference of window as a 1st argument to the constructor of component layout functions.
- some keywords like = text, fg, bg, font, command
Ex-
from tkinter import Tk, Label
window = Tk()
l = Label(window, text="simpleAlgo", fg="Blue", bg="Yellow", font="25")
l.pack()
window.mainloop()
Geometry()-
- – geometry is the predefined method of Tk class.
- – It is used to set the width and height of window.
Ex-
from tkinter import Tk, Button
def demo():
print("simpleAlgo")
window = Tk()
window.geometry("300x400") #width x height
b = Button(window, text="click Here", command=demo)
b.pack()
window.mainloop()
Ex-
from tkinter import Tk, Button, Label, Entry
def demo():
name= e.get()
print(name)
window = Tk()
window.geometry("850x250") #width and Height
l = Label(window, text="Enter Name")
l.place(x=70, y=70)
e = Entry(window, font="Times 20")
e.place(x=300, y=70)
b= Button(window, text="Click Here", command=demo)
b.place(x=700, y=70)
window.mainloop()
Ex-
from tkinter import Tk, Label, StringVar
root = Tk()
obj = StringVar()
l = Label(root, textvariable=obj)
age = input("Enter your age-")
obj.set(age)
l.pack()
root.mainloop()
Ex-
from tkinter import *
def test():
fn = e1.get()
ln = e2.get()
obj.set("Hello "+fn+" "+ln)
window = Tk()
window.geometry("270x150") #width and Height
obj=StringVar()
l1 = Label(window, text = "Enter first name")
l2 = Label(window, text = "Enter last name")
e1 = Entry(window, fg="Blue", font="Courier 10")
e2 = Entry(window, fg="Blue", font="Courier 10")
b1 = Button(window, text="Click Here", command=test)
disp = Label(window, textvariable=obj)
l1.grid(row=0, column=0)
l2.grid(row=1, column=0)
disp.grid(row=2, column=0)
b1.grid(row=3, column=0)
e1.grid(row=0, column=1)
e2.grid(row=1, column=1)
window.mainloop()