2 years ago

#63926

test-img

Sudalai Vignesh

How To add scroll bar in the f3 in tkinter treeview

How To add scroll bar in the f3 in tkinter treeview The below code is just to insert student details,update student details,delete student detail, and clear the info in the entry box. But my problem is after doing all this operation i will display the results in a seperate frame but I am not able to add scrollbar with this tkinter code.Actually the code isn't completed have to extend more

from tkinter import *
from tkinter import ttk
from tkinter import messagebox
from db import Database
import datetime
db=Database("students.db")
#functions
def datecheck():
    
    a=True
    try:
        day,month,year=txtdoa.get().split('/')
        datetime.datetime(int(year),int(month),int(day))
    except ValueError:
        a=False
    return a
def check():
    a=False
    if not (txtage.get().isdigit() or len(txtage.get())==2)or not datecheck():
        a=True
    elif not (txtsno.get().isdigit  and  len(txtsno.get())==10):
        a=True
    elif not (txtpno.get().isdigit  and  len(txtpno.get())==10):
        a=True
    return a
def getdata(event):
    selectedrow=t.focus()
    data=t.item(selectedrow)
    global row
    row=data["values"]
    name.set(row[1])
    age.set(row[2])
    doa.set(row[4])
    gender.set(row[3])
    sno.set(row[5])
    pno.set(row[6])
    txtad.delete(1.0,END)
    txtad.insert(END,row[7])
def disp():
    t.delete(*t.get_children())
    for row in db.fetch():
        t.insert("",END,values=row)
def addinfo():
    if txtname.get()=="" or txtage.get()=="" or txtdoa.get()=="" or txtsno.get()=="" or txtpno.get()=="" or combogender.get()=="" or txtad.compare("end-1c","==","1.0"):
        messagebox.showerror("Alert","Please fill All the Info")
        return
    db.insert(txtname.get(),txtage.get(),combogender.get(),txtdoa.get(),txtsno.get(),txtpno.get(),txtad.get(1.0,END))
    messagebox.showinfo("Sucess","Inserted Successfully")
    clear()
    disp()
    
def updateinfo():
    if txtname.get()=="" or txtage.get()=="" or txtdoa.get()=="" or txtsno.get()=="" or txtpno.get()=="" or combogender.get()=="" or txtad.compare("end-1c","==","1.0"):
        messagebox.showerror("Alert","Please fill All the Info")
        return
    elif check():
        messagebox.showerror("Alert","Please Check the Details")
        return
    db.update(row[0],txtname.get(),txtage.get(),combogender.get(),txtdoa.get(),txtsno.get(),txtpno.get(),txtad.get(1.0,END))
    messagebox.showinfo("Sucess","Record Updated")
    clear()
    disp()
def deleteinfo():
    db.remove(row[0])
    messagebox.showinfo("Success","Removed Successfully")
    clear()
    disp()
def clear():
    name.set("")
    age.set("")
    doa.set("")
    gender.set("")
    doa.set("")
    sno.set("")
    pno.set("")
    txtad.delete(1.0,END)

def page1hide():
    f1.grid_forget()
    f3.grid_forget()
def page1show():
    f1.grid(row=1,column=0,sticky='w',ipadx=300)
    f3.grid(row=2,column=0,sticky='w',ipadx=350)
    #global f3=Frame(root,bg="White")

#window
root=Tk()
root.title("Students Details")
root.geometry("1920x1080")
root.config(bg="#ff0000")
root.state("zoomed")

name=StringVar()
age=StringVar()
doa=StringVar()
gender=StringVar()
doa=StringVar()
sno=StringVar()
pno=StringVar()
address=StringVar()
#frme
f=Frame(root,bg="#800040")
f.grid(row=0,column=0)
#frame1
f1=Frame(root,bg="#ffcccc")
f1.grid(row=1,column=0,sticky='w',ipadx=300)
#title
title=Label(f1,text="Students Details",font=("Tahoma",18,"bold"),bg="#ffcccc",fg="black")
title.grid(row=0,columnspan=2,padx=10,pady=20,sticky="w")
#name
lbn=Label(f1,text="Name",font=("Tahoma",16),bg="#ffcccc",fg="black")
lbn.grid(row=1,column=0,padx=10,pady=10,sticky="w")
txtname=Entry(f1,textvariable=name,font=("Tahoma",16),width=30)
txtname.grid(row=1,column=1,padx=10,pady=10,sticky="w")
#age
lbage=Label(f1,text="Age",font=("Tahoma",16),bg="#ffcccc",fg="black")
lbage.grid(row=1,column=2,padx=10,pady=10,sticky="w")
txtage=Entry(f1,textvariable=age,font=("Tahoma",16),width=30)
txtage.grid(row=1,column=3,padx=10,pady=10,sticky="w")
#doa
lbdoa=Label(f1,text="Date Of Admission\n(DD/MM/YYYY)",font=("Tahoma",16),bg="#ffcccc",fg="black")
lbdoa.grid(row=2,column=0,padx=10,pady=10,sticky="w")
txtdoa=Entry(f1,textvariable=doa,font=("Tahoma",16),width=30)
txtdoa.grid(row=2,column=1,padx=10,pady=10,sticky="w")
#gender
lbgender=Label(f1,text="Gender",font=("Tahoma",16),bg="#ffcccc",fg="black")
lbgender.grid(row=2,column=2,padx=10,pady=10,sticky="w")
combogender=ttk.Combobox(f1,textvariable=gender,font=("Tahoma",16),width=28,state="readonly")
combogender["values"]=("Male","Female")
combogender.grid(row=2,column=3,padx=10,pady=10,sticky="w")
#sno
lbsno=Label(f1,text="Student Phno.",font=("Tahoma",16),bg="#ffcccc",fg="black")
lbsno.grid(row=3,column=0,padx=10,pady=10,sticky="w")
txtsno=Entry(f1,textvariable=sno,font=("Tahoma",16),width=30)
txtsno.grid(row=3,column=1,padx=10,pady=10,sticky="w")
#phno
lbpno=Label(f1,text="Parent Phno.",font=("Tahoma",16),bg="#ffcccc",fg="black")
lbpno.grid(row=3,column=2,padx=10,pady=10,sticky="w")
txtpno=Entry(f1,textvariable=pno,font=("Tahoma",16),width=30)
txtpno.grid(row=3,column=3,padx=10,pady=10,sticky="w")
#address
lbad=Label(f1,text="Address",font=("Tahoma",16),bg="#ffcccc",fg="black")
lbad.grid(row=4,column=0,padx=10,pady=10,sticky="w")
txtad=Text(f1,height=5,width=75,font=("Tahoma",16))
txtad.grid(row=4,column=1,columnspan=4,padx=10,pady=10,sticky="w")
#buttonframe
f2=Frame(f1,bg="#ffcccc")
f2.grid(row=5,column=1,columnspan=4,padx=10,pady=10,sticky="w")
btnadd=Button(f2,command=addinfo,text="Add Info",width=15,font=("Tahoma",16,"bold"),bg="#16a085",fg="white",bd=0).grid(row=0,column=0,padx=10)
btnupdate=Button(f2,command=updateinfo,text="Update Info",width=15,font=("Tahoma",16,"bold"),bg="#2980b9",fg="white",bd=0).grid(row=0,column=1,padx=10)
btndel=Button(f2,command=deleteinfo,text="Delete Info",width=15,font=("Tahoma",16,"bold"),bg="#c0392b",fg="white",bd=0).grid(row=0,column=2,padx=10)
btnclear=Button(f2,command=clear,text="Clear Info",width=15,font=("Tahoma",16,"bold"),bg="#f39c12",fg="white",bd=0).grid(row=0,column=3,padx=10)
#frame3
f3=Frame(root,bg="White")
f3.grid(row=2,column=0,sticky='w',ipadx=350)
style=ttk.Style()
style.configure("mystyle.Treeview",font=("Tahoma",16),rowheight=50)
style.configure("mystyle.Treeview.Heading",font=("Tahoma",18,'bold'))
#table
t=ttk.Treeview(f3,columns=(1,2,3,4,5,6,7,8),style="mystyle.Treeview")
t.heading("1",text="ID")
t.column('1',width=5)
t.heading("2",text="Name")
t.column('2',width=20)
t.heading("3",text="Age")
t.column('3',width=5)
t.heading("4",text="Gender")
t.column('4',width=10)
t.heading("5",text="Admission Date")

t.heading("6",text="Student Phno.")

t.heading("7",text="Parent Phno.")

t.heading("8",text="Address")

t['show']='headings'
t.bind("<ButtonRelease-1>",getdata)
t.pack(fill=X)

disp()


root.mainloop()

In the above code I dont know how to enable scroll bar for treeview.Actually I need for that particular frame f3 because it displays the info from database. can anyone help me to get out of this.

python

user-interface

tkinter

treeview

scrollbar

0 Answers

Your Answer

Accepted video resources