PYTHON-GUI PROGRAMMING [TKINTER]
CREATE WINDOW
import
Tkinter
win=Tkinter.Tk()
win.mainloop()
Output
LABEL WIDGET
For example:-
from
Tkinter import*
master
=Tk()
w=Label(master,
text="Welcome to the world")
w.pack()
mainloop()
Output
Example:-
#Example for label with
font and colour
from
Tkinter import*
master=Tk()
w=Label(master,
text="Welcome",fg="red",font=(" Arial Unicode
MS",40))
w.pack()
mainloop()
Output
Example:-
#multiline text with
label wrap
#font size and justify
features
from
Tkinter import*
master=Tk()
v=StringVar()
w=Label(master,
textvariable=v, fg="red", font=("verdana",14), anchor=W,
justify=CENTER)
v.set("label
can display multiple line of text.\nyou can use newline or use the wraplength
option \nto make the label wrap text by itself.")
w.pack()
mainloop()
Output
Printing image in Tkinter
Example:-
from
Tkinter import*
root=Tk()
Photo=PhotoImage(file="c:\python27\logo.gif")
w=label(root,image=photo)
w.pack()
root.mainloop()
LabelFrame Widget
from
Tkinter import*
master=Tk()
group=LabelFrame(master,text="E-mail
id", padx=5, pady=5)
group.pack(padx=10,pady=10)
w=Entry(group)
w.pack()
mainloop()
Output
For Example:-
# fetch the entry item
into labelframe
from
Tkinter import*
master=Tk()
group=LabelFrame(master,text="E-Mail
ID",bd=5,padx=10,pady=10,bg="blue",fg="red")
group.pack(padx=10,pady=10)
w=Entry(group)
w.pack()
mainloop()
Output
BUTTON WIDGET
syntax:-
Button(master=None,**options)
for example1:-
from
Tkinter import*
master=Tk()
def
DispFun():
print("Welcome Student")
b=Button(master,text="press
me",command=DispFun)
b.pack()
mainloop()
Output
Welcome
Student
for example2:-
from
Tkinter import *
master=Tk()
def
dispFun():
print("\n"*50)
print("Hello World!")
b=Button(master,text="Press
Me"
,padx=20,pady=20,command=dispFun,bg="red",fg="green",
font=("verdana",20),relief=FLAT)
b.pack()
mainloop()
Output
============================ RESTART: E:/tkk8.py ============================
Hello
World!
Example:-
from
Tkinter import*
master=Tk()
var1 =
IntVar()
var2 =
IntVar()
var3 =
IntVar()
var4 =
IntVar()
var5 =
IntVar()
def
checkfun():
txt=StringVar
txt=" "
if var1.get()==1:
txt =txt +" " +
c1.cget("text")
if var2.get()==1:
txt =txt +" " +
c2.cget("text")
if var3.get()==1:
txt =txt +" " +
c3.cget("text")
if var4.get()==1:
txt =txt +" " +
c4.cget("text")
if var5.get()==1:
txt =txt +" " +
c5.cget("text")
print(txt)
c1 =
Checkbutton(master,text="HDCA",variable=var1)
c2 =
Checkbutton(master,text="HDCP",variable=var2)
c3 =
Checkbutton(master,text="ADJP",variable=var3)
c4 =
Checkbutton(master,text="DUC",variable=var4)
c5 =
Checkbutton(master,text="DCA",variable=var5)
b1 =
Button(master,text="OK",command=checkfun)
c1.pack()
c2.pack()
c3.pack()
c4.pack()
c5.pack()
b1.pack()
mainloop()
Output
Python - Tkinter Spinbox
from
Tkinter import *
master
= Tk()
sptxt
= StringVar(master)
sptxt.set("DMO")
def
callme():
print sptxt .get()
w
= Spinbox(master,textvariable=
sptxt,value=("HDCA","DCA","DMO","ADJP","DUC","J2EE","DTP")
,fg="green",command=callme,bd=4)
sptxt.set("DMO")
w.pack()
mainloop()
Output
Listbox Widget
Example for Listbox widget
from
Tkinter import *
master
= Tk()
txt=StringVar()
listbox
= Listbox(master)
listbox.pack()
for
item in["HDCA","DCA","DMO","DOTNET"]:
listbox.insert(END,item)
def
callback():
txt.set(listbox.get(ACTIVE))
Label(master,text="selected
Item:").pack()
Entry(master,text="",textvariable=txt).pack()
button
= Button(master,text="press",command=callback)
button.pack()
mainloop()
Output
Menu Widget
Example
from
Tkinter import*
root
= TK()
def
hello():
print"hello!"
menubar
= Menu(root)
create
a pulldown menu, and add it to the menu bar
filemenu = Menu(menubar,tearoff=0)
filemenu.add_command(lable="Open",command=hello)
filemenu.add_command(lable="Save",command=hello)
filemenu.add_separator()
filemenu.add_command(lable="Exit",command=root.quit)
menubar.add_cascade(lable="File",menu=filemenu)
from
Tkinter import*
root
= Tk()
def
hello():
print("hello!")
menubar
= Menu(root)
#create
a pulldown menu, and add it to the menu bar
filemenu = Menu(menubar,tearoff=0)
filemenu.add_command(label="Open",command=hello)
filemenu.add_command(label="Save",command=hello)
filemenu.add_separator()
filemenu.add_command(label="Exit",command=root.quit)
menubar.add_cascade(label="File",menu=filemenu)
#create
more pulldown menus
editmenu
= Menu(menubar,tearoff=0)
editmenu.add_command(label="Cut",command=hello)
editmenu.add_command(label="Copy",command=hello)
editmenu.add_command(label="Paste",command=hello)
menubar.add_cascade(label="Edit",
menu=editmenu)
helpmenu=
Menu(menubar,tearoff=0)
helpmenu.add_command(label="About",command=hello)
menubar.add_cascade(label="Help",menu=helpmenu)
#display
the menu
root.config(menu=menubar)
mainloop()
output
#OptionMenu Widget
the constructor syntax is:
from
Tkinter import*
#OptionMenu(master,variable,*values)
OPTIONS
= [
"HDCA",
"TALLY",
"PYTHON"
]
master
= Tk()
variable
= StringVar(master)
variable.set(OPTIONS[0])
# defalut value
w
= apply(OptionMenu,(master,variable)+ tuple(OPTIONS))
w.pack()
mainloop()
output
Radiobutton Widget
from
Tkinter import*
master
= Tk()
MODES
= [
("HDCA","H"),
("DCA","D"),
("TALLY","T"),
("AUTOCAD","A"),
]
v
= StringVar()
v.set("T")
# intialize
for
text, mode in MODES:
b =
Radiobutton(master,text=text,variable=v, value=mode)
b.pack(anchor=W)
mainloop()
Output
Scale Widget
from
Tkinter import *
master
= Tk()
w
= Scale(master,from_=0,to=100)
w.pack()
w
= Scale(master,from_=0, to=200, orient=HORIZONTAL)
w.pack()
mainloop()
Output
Example:-
from
Tkinter import *
master
= Tk()
s1=IntVar()
s2=IntVar()
st1=StringVar()
st2=StringVar()
s1.set(5)
s2.set(3)
def
scalefun1(*args):
st1.set(s1.get())
def
scalefun2(*args):
st2.set(s2.get())
Scale1
= Scale(master,variable=s1,from_=0,to=100,command=scalefun1)
Scale1.pack()
Entry(master,textvariable=st1).pack()
Entry(master,textvariable=st2).pack()
Scale2
= Scale(master,variable=s2,from_=0,to=200,orient=HORIZONTAL,command=scalefun2)
Scale2.pack()
mainloop()
Tkinter
tkmessageBox
Output
Tkinter tkMessageBox
from
Tkinter import*
import
tkMessageBox
master
= Tk()
num
=StringVar()
def
callme():
if int(num.get())<0:
tkMessageBox.showinfo("Error","Enter postive
number")
else:
tkMessageBox.showinfo("Welcome","Vaild number")
Label(master,text="Enter
a number").pack()
Entry(master,textvariable=num).pack()
B1
= Button(master,text="Welcome",command =callme)
B1.pack()
mainloop()
Output
Scrollbar Widget
Example
from
Tkinter import*
master
= Tk()
lst=['Kannadasan','rajesh','mahadevan','seetha
rama chandren','leela',\
'samuel','kumar','malathy','balaraman','revathi','sumathi',\
'kaviya','arunkumar','sujatha']
scrollbar
= Scrollbar(master,orient=HORIZONTAL)
scrollbar.pack(side=BOTTOM,
fill=BOTH)
listbox
= Listbox(master, yscrollcommand=scrollbar.set)
for
i in lst:
listbox.insert(END,i)
listbox.pack(side=LEFT)
scrollbar.config(command=listbox.yview)
mainloop()
Output
Insert Data into MYSQL
Database Using Tkinter
Data Entry
Screen
import Tkinter
from Tkinter import*
import sqlite3,sys
# open database connection
# prepare a cursor object using cursor() method
conn=sqlite3.connect("emplo.db")
cursor=conn.cursor()
master = Tk()
group=LabelFrame(master,text=" E M P L O Y E E D A T A E N T R Y S C R E E N", padx=10,pady=10)
group.pack(padx=10, pady=10)
T1 =StringVar()
T2 =StringVar()
T3 =StringVar()
T4 =IntVar()
c1 =StringVar()
Tsex = StringVar()
def callme():
if c1.get()=="M":
Tsex="Male"
else:
Text="Female"
sql = "insert into cscemp(empno,empname,sex,desig,salary)values('%d','%s','%s','%d')"%((int(T1.get()),T2.get(),Tsex,T3.get(),(int(T4.get()))))
cursor.execute(sql)
db.commit()
Label(group,text="Employee No").grid(row=0,column=3)
w=Entry(group,textvariable=T1).grid(row=0,column=20)
Label(group,text="Employee Name").grid(row=2,column=3)
w1=Entry(group,textvariable=T2).grid(row=2,column=20)
Label(group,text="Designation").grid(row=6,column=3)
w3=Entry(group,textvariable=T3).grid(row=6,column=20)
Label(group,text="Sex").grid(row=4,column=3)
R1 = Radiobutton(group,text="Male", variable=c1,value="M").grid(row=4,column=20)
R2 = Radiobutton(group,text="Female", variable=c1,value="F").grid(row=4,column=25)
b1 = Button(group,text="Add",command=callme).grid(row=7,column=25)
w4=Entry(group,textvariable=T4).grid(row=8,column=20)
Label(group,text="Salary").grid(row=8,column=3)
c1.set("M")
mainloop()
db.close()
from Tkinter import *
import sqlite3,sys
def connection():
try:
conn=sqlite3.connect("student.db")
except:
print("cannot connect to the database")
return
conn
def verifier():
a=b=c=d=e=f=0
if not
student_name.get():
t1.insert(END,"<>Student name is required<>\n")
a=1
if not
roll_no.get():
t1.insert(END,"<>Roll no is required<>\n")
b=1
if not
branch.get():
t1.insert(END,"<>Branch is required<>\n")
c=1
if not
phone.get():
t1.insert(END,"<>Phone number is requrired<>\n")
d=1
if not
father.get():
t1.insert(END,"<>Father name is required<>\n")
e=1
if not
address.get():
t1.insert(END,"<>Address is Required<>\n")
f=1
if a==1 or
b==1 or c==1 or d==1 or e==1 or f==1:
return 1
else:
return 0
def add_student():
ret=verifier()
if
ret==0:
conn=connection()
cur=conn.cursor()
cur.execute("CREATE TABLE IF NOT EXISTS STUDENTS(NAME TEXT,ROLL_NO
INTEGER,BRANCH TEXT,PHONE_NO INTEGER,FATHER TEXT,ADDRESS TEXT)")
cur.execute("insert into STUDENTS values(?,?,?,?,?,?)",(student_name.get(),int(roll_no.get()),branch.get(),int(phone.get()),father.get(),address.get()))
conn.commit()
conn.close()
t1.insert(END,"ADDED SUCCESSFULLY\n")
def view_student():
conn=connection()
cur=conn.cursor()
cur.execute("select * from STUDENTS")
data=cur.fetchall()
conn.close()
for i in
data:
t1.insert(END,str(i)+"\n")
def delete_student():
ret=verifier()
if ret==0:
conn=connection()
cur=conn.cursor()
cur.execute("DELETE FROM STUDENTS WHERE
ROLL_NO=?",(int(roll_no.get()),))
conn.commit()
conn.close()
t1.insert(END,"SUCCESSFULLY DELETED THE USER\n")
def update_student():
ret=verifier()
if ret==0:
conn=connection()
cur=conn.cursor()
cur.execute("UPDATE STUDENTS SET
NAME=?,ROLL_NO=?,BRANCH=?,PHONE_NO=?,FATHER=?,ADDRESS=? where
ROLL_NO=?",(student_name.get(),int(roll_no.get()),branch.get(),int(phone.get()),father.get(),address.get(),int(roll_no.get())))
conn.commit()
conn.close()
t1.insert(END,"UPDATED SUCCESSFULLY\n")
def clse():
sys.exit()
if __name__=="__main__":
root=Tk()
root.title("Student Management System")
student_name=StringVar()
roll_no=StringVar()
branch=StringVar()
phone=StringVar()
father=StringVar()
address=StringVar()
label1=Label(root,text="Student name:")
label1.place(x=0,y=0)
label2=Label(root,text="Roll no:")
label2.place(x=0,y=30)
label3=Label(root,text="Branch:")
label3.place(x=0,y=60)
label4=Label(root,text="Phone Number:")
label4.place(x=0,y=90)
label5=Label(root,text="Father Name:")
label5.place(x=0,y=120)
label6=Label(root,text="Address:")
label6.place(x=0,y=150)
e1=Entry(root,textvariable=student_name)
e1.place(x=100,y=0)
e2=Entry(root,textvariable=roll_no)
e2.place(x=100,y=30)
e3=Entry(root,textvariable=branch)
e3.place(x=100,y=60)
e4=Entry(root,textvariable=phone)
e4.place(x=100,y=90)
e5=Entry(root,textvariable=father)
e5.place(x=100,y=120)
e6=Entry(root,textvariable=address)
e6.place(x=100,y=150)
t1=Text(root,width=80,height=20)
t1.grid(row=10,column=1)
b1=Button(root,text="ADD
STUDENT",command=add_student,width=40)
b1.grid(row=11,column=0)
b2=Button(root,text="VIEW ALL
STUDENTS",command=view_student,width=40)
b2.grid(row=12,column=0)
b3=Button(root,text="DELETE STUDENT",command=delete_student,width=40)
b3.grid(row=13,column=0)
b4=Button(root,text="UPDATE
INFO",command=update_student,width=40)
b4.grid(row=14,column=0)
b5=Button(root,text="CLOSE",command=clse,width=40)
b5.grid(row=15,column=0)
root.mainloop()
0 comments:
Post a Comment