You are on page 1of 11

Brindha 1901029

TKINTER PROGRAMS

1. Develop a simple calculator application using tkinter and show output for all basic operations.

Program:

from tkinter import *

import tkinter as tk

def calculate():

if operation.get() == 1:

res=int(value1.get())+int(value2.get())

elif operation.get() == 2:

res=int(value1.get())-int(value2.get())

elif operation.get() == 3:

res=int(value1.get())*int(value2.get())

elif operation.get() == 4:

res=int(value1.get())/int(value2.get())

else:

res ="check radio button"

result.set(res)

window = Tk()

window.title('Calculator')

window.geometry('300x300')

operation =IntVar()

result=StringVar()
Brindha 1901029

value1 =tk.StringVar()

value2 =StringVar()

Label(window, text="FIRST NUMBER",fg="black" ).grid(row=1,column = 0)

Entry(window,textvariable = value1).grid(row=1, column=1)

Label(window,text="SECOND NUMBER",fg="black" ).grid(row=2,column=0)

Entry(window,textvariable = value2).grid(row=2, column=1)

Label(window,text="RESULT",fg="blue").grid(row=240,column=0)

Button(window,textvariable=result,bg="green").grid(row=241, column=0)

Radiobutton(window,text="+",variable=operation, value=1).grid(row=3, column=0)

Radiobutton(window,text="-",variable=operation, value=2).grid(row=3, column=1)

Radiobutton(window,text="*",variable=operation, value=3).grid(row=4, column=0)

Radiobutton(window,text="/",variable=operation, value=4).grid(row=4, column=1)

Button(window,text="Calculate", command=calculate,bg="pink",fg="black").grid(row=4, column=4)

window.mainloop()

OUTPUT:
Brindha 1901029

2.  Create a tkinter application to open a text file and print the contents of the file in a tkinter window.

PROGRAM:

from tkinter import *

from tkinter import filedialog

def openFile():

tf = filedialog.askopenfilename(

initialdir="C:/Users/Brindha/Documents/",

title="Open Text file",

filetypes=(("Text Files", "*.txt"),)

path.insert(END, tf)

tf = open(tf)

data = tf.read()

txtarea.insert(END, data)

tf.close()

window = Tk()

window.title("File in Tkinter")

window.geometry("500x450")

window['bg']='pink'

txtarea = Text(window, width=50, height=20)

txtarea.pack(pady=20)

path = Entry(window)

path.pack(side=LEFT, expand=True, fill=X, padx=20)

Button(window, text="Open File", command=openFile).pack(side=RIGHT, expand=True, fill=X, padx=20)


Brindha 1901029

window.mainloop()

Tkinter file.txt

Tkinter is a Python binding to the Tk GUI toolkit.

It is the standard Python interface to the Tk GUI toolkit.

Tkinter is included with standard Linux, Microsoft Windows and Mac OS X installs of Python.

Do checkout out Tkinter!

OUTPUT:

3. Create a course registration form containing all possible widgets using various GUI’s like Tkinter,

Tk Interface eXtensions (Tix), Python MegaWidgets (PMW) and Tile/Ttk. Also create a DB and a

table for maintaining the course registration details. If u apply for a course using GUI, the details

should be saved to the Data Base.


Brindha 1901029

PROGRAM:

from tkinter import *

from tkinter import ttk

from tkcalendar import DateEntry

import mysql.connector

conn = mysql.connector.connect(user='root', password='', host='localhost', database='Registration

Form')

def database():

#getting form data

name1=name.get()

dob1=dob.get()

gen1=gender.get()

email1=email.get()

mobile1=mobile.get()

addr1=addr.get()

course1=course.get()

reason1=reason.get()

if name1=='' or dob1==''or gen1=='' or email1=='' or mobile1=='' or addr1=='' or reason1=='' or

course1=='':
Brindha 1901029

message.set("Fill the empty field!!!")

else:

cursor = conn.cursor()

insert_stmt = ("INSERT INTO

information(NAME,DOB,GENDER,MOBILE,EMAIL,ADDRESS,COURSE,REASON) VALUES

(%s,%s,%s,%s,%s,%s,%s,%s)")

data = (name1,dob1,gen1,mobile1,email1,addr1,course1,reason1)

try:

cursor.execute(insert_stmt,data)

conn.commit()

except:

conn.rollback()

message.set("Successfully registered for the course!")

window = Tk()

window.title('CourseRegistration')

window.geometry('400x400')

window.configure(background="light blue")
Brindha 1901029

global name

global dob

global email

global gender

global mobile

global addr

global course

global reason

global message

name = StringVar()

dob = StringVar()

email=StringVar()

gender=StringVar()

mobile=StringVar()

addr=StringVar()

reason=StringVar()

course=StringVar()

message=StringVar()
Brindha 1901029

Label(window ,text = "Name").grid(row = 0,column = 0)

Entry(window,textvariable=name).grid(row = 0,column = 1)

Label(window ,text = "DOB").grid(row = 1,column = 0,padx = 5, pady = 10)

DateEntry(window,textvariable=dob, width=17, year=2021, month=9, day=17,background='pink',

foreground='white', borderwidth=2).grid(row = 1,column = 1)

Label(window, text = "Gender",font = ("Times New Roman", 10)).grid(column = 0,row = 2, padx =

5, pady = 10)

gender= ttk.Combobox(window, width = 17, textvariable = gender)

gender['values'] = ('Male','Female')

gender.grid(column = 1, row = 2)

gender.current()

Label(window ,text = "Mobile Number").grid(row = 3,column = 0,padx = 5, pady = 10)

Entry(window,textvariable=mobile).grid(row = 3,column = 1)

Label(window ,text = "Email id").grid(row = 4,column = 0,padx = 5, pady = 10)

Entry(window,textvariable=email).grid(row = 4,column = 1)

Label(window ,text = "Address").grid(row = 5,column = 0,padx = 5, pady = 10)

Entry(window,textvariable=addr).grid(row = 5,column = 1)
Brindha 1901029

Label(window, text = "Course Choice",font = ("Times New Roman", 10)).grid(column = 0,row = 6,

padx = 5, pady = 10)

CourseChoice= ttk.Combobox(window, width = 17, textvariable = course)

CourseChoice['values'] = ('Python Applications', 'Computer Vision','Deep Learning','C

Programming','Artificial Intelligence')

CourseChoice.grid(column = 1, row = 6)

CourseChoice.current()

Label(window ,text = "Reason for choosing the course").grid(row = 7,column = 0,padx = 5, pady =

10)

Entry(window,textvariable=reason).grid(row = 7,column = 1)

Label(window, text="",textvariable=message,bg="pink",fg="red").grid(row = 8,column = 0,padx =

10, pady = 10)

Button(window,text="Submit",command=database).grid(row=10,column=0,padx = 5, pady = 10)

window.mainloop()
Brindha 1901029

OUTPUT:
Brindha 1901029

DATABASE:

Database: Registration

Table name: Information

You might also like