Tkinter Examples Using Python3

1. Simple tkinter Program To Generate Window using Python3.

from tkinter import *;

tt=Tk()
tt.mainloop()
tt.geometry("1000X1000")

2. Simple tkinter Program To Display A Button On Window using Python3.

from tkinter import *

tt=Tk()
#tt.geometry('100X100')

b=Button(tt,text="Clickme")
b.place(x=10,y=20)
tt.mainloop()

3. Simple tkinter Program To Call Display Function On Click Of A Button On Window using Python3.

from tkinter import *

tt=Tk()

def display():
    print("HEllo Welcome")

b=Button(tt,text="Clickme",command=display)
b.place(x=10,y=20)
tt.mainloop()

4. Simple tkinter Program To Call Display Function And Display Types Of Message Boxes On Click Of A Button On Window using Python3.

from tkinter import *
from tkinter import messagebox

tt=Tk()

def display():
    messagebox.showerror("Error Message","This Is An Error")
    messagebox.showwarning("Warning Message","This Is A Warning")
    messagebox.showinfo("Information Message","This Is A Information")

b=Button(tt,text="Clickme",command=display)
b.place(x=10,y=20)
tt.mainloop()

5. Simple tkinter Program To Perform Addition Of Two Inputs Entered Into Text Fields using Python3.

from tkinter import *
tt=Tk()

def display():
    a=int(t1.get("1.0","end"))
    b=int(t2.get("1.0","end"))
    c=a+b
    t3.delete("1.0","end")
    t3.insert("1.0",c)
    #print(c)


b=Button(tt,text="Clickme",command=display)
b.place(x=10,y=20)

l1=Label(tt,text="First Value")
t1=Text(tt,width=10,height=1)
l1.place(x=200,y=20)
t1.place(x=450,y=20)

l2=Label(tt,text="Second Value")

t2=Text(tt,width=10,height=1)
l2.place(x=200,y=40)
t2.place(x=450,y=40)

l3=Label(tt,text="Third Value")

t3=Text(tt,width=10,height=1)
l3.place(x=200,y=60)
t3.place(x=450,y=60)

tt.mainloop()


6. Simple tkinter Program To Perform Addition Of Two Inputs Entered Into Text Fields With User Confirmation using Python3.

from tkinter import *
from tkinter import messagebox

tt=Tk()

def display():
    a=int(t1.get("1.0","end"))
    b=int(t2.get("1.0","end"))

    c = a + b
    if(messagebox.askyesno("Do U want To Continue",
                           "Do U Want Addition")):
       t3.delete("1.0","end")
       t3.insert("1.0",c)
    else:
       t3.delete("1.0", "end")
       #t3.insert("1.0", "")



b=Button(tt,text="Clickme",command=display)

b.place(x=10,y=20)

l1=Label(tt,text="First Value")
t1=Text(tt,width=10,height=1)
l1.place(x=200,y=20)
t1.place(x=450,y=20)

l2=Label(tt,text="Second Value")

t2=Text(tt,width=10,height=1)
l2.place(x=200,y=40)
t2.place(x=450,y=40)
l3=Label(tt,text="Third Value")

t3=Text(tt,width=10,height=1)
l3.place(x=200,y=60)
t3.place(x=450,y=60)

tt.mainloop()


7. Simple tkinter Program To Read Content From Text Area using Python3.

from tkinter import *
from tkinter import messagebox

tt=Tk()

def display():
    messagebox.showinfo("Data Is",ta.get("1.0","end"))


b=Button(tt,text="Clickme",command=display)
b.place(x=10,y=20)
ta=Text(tt,width=10,height=10)
ta.place(x=100,y=200)
tt.mainloop()


8. Simple tkinter Program To Read Content From Text Area using Python3.

from tkinter import *
from tkinter import messagebox

tt=Tk()

def display():
    messagebox.showinfo("Data Is",ta.get("1.0","end"))


b=Button(tt,text="Clickme",command=display)
b.place(x=10,y=20)
ta=Text(tt,width=10,height=10)
ta.place(x=100,y=200)
tt.mainloop()