Skip to content

Commit 48a8316

Browse files
committed
How To Create Treeview Scrollbar With Python Tkinter
How To Create Treeview Scrollbar With Python Tkinter
1 parent 064e207 commit 48a8316

File tree

1 file changed

+69
-26
lines changed

1 file changed

+69
-26
lines changed

Diff for: main.py

+69-26
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,78 @@
11

2-
# Import all files from
3-
# tkinter and overwrite
4-
# all the tkinter files
5-
# by tkinter.ttk
6-
from tkinter import *
7-
from tkinter.ttk import *
2+
# Python program to illustrate the usage of
3+
# treeview scrollbars using tkinter
84

9-
# creates tkinter window or root window
10-
root = Tk()
11-
root.geometry('500x500')
125

13-
# function to be called when button-2 of mouse is pressed
14-
def pressed2(event):
15-
print('Button-2 pressed at x = % d, y = % d'%(event.x, event.y))
6+
from tkinter import ttk
7+
import tkinter as tk
168

17-
# function to be called when button-3 of mouse is pressed
18-
def pressed3(event):
19-
print('Button-3 pressed at x = % d, y = % d'%(event.x, event.y))
9+
# Creating tkinter window
10+
window = tk.Tk()
11+
window.resizable(width = 1, height = 1)
2012

21-
## function to be called when button-1 is double clocked
22-
def double_click(event):
23-
print('Double clicked at x = % d, y = % d'%(event.x, event.y))
13+
# Using treeview widget
14+
treev = ttk.Treeview(window, selectmode ='browse')
2415

25-
frame1 = Frame(root, height = 500, width = 500)
16+
# Calling pack method w.r.to treeview
17+
treev.pack(side ='right')
2618

27-
# these lines are binding mouse
28-
# buttons with the Frame widget
29-
frame1.bind('<Button-2>', pressed2)
30-
frame1.bind('<Button-3>', pressed3)
31-
frame1.bind('<Double 1>', double_click)
19+
# Constructing vertical scrollbar
20+
# with treeview
21+
verscrlbar = ttk.Scrollbar(window,
22+
orient ="vertical",
23+
command = treev.yview)
3224

33-
frame1.pack()
25+
# Calling pack method w.r.to vertical
26+
# scrollbar
27+
verscrlbar.pack(side ='right', fill ='x')
3428

35-
mainloop()
29+
# Configuring treeview
30+
treev.configure(xscrollcommand = verscrlbar.set)
31+
32+
# Defining number of columns
33+
treev["columns"] = ("1", "2", "3")
34+
35+
# Defining heading
36+
treev['show'] = 'headings'
37+
38+
# Assigning the width and anchor to the
39+
# respective columns
40+
treev.column("1", width = 90, anchor ='c')
41+
treev.column("2", width = 90, anchor ='se')
42+
treev.column("3", width = 90, anchor ='se')
43+
44+
# Assigning the heading names to the
45+
# respective columns
46+
treev.heading("1", text ="Name")
47+
treev.heading("2", text ="Sex")
48+
treev.heading("3", text ="Age")
49+
50+
# Inserting the items and their features to the
51+
# columns built
52+
treev.insert("", 'end', text ="L1",
53+
values =("Nidhi", "F", "25"))
54+
treev.insert("", 'end', text ="L2",
55+
values =("Nisha", "F", "23"))
56+
treev.insert("", 'end', text ="L3",
57+
values =("Preeti", "F", "27"))
58+
treev.insert("", 'end', text ="L4",
59+
values =("Rahul", "M", "20"))
60+
treev.insert("", 'end', text ="L5",
61+
values =("Sonu", "F", "18"))
62+
treev.insert("", 'end', text ="L6",
63+
values =("Rohit", "M", "19"))
64+
treev.insert("", 'end', text ="L7",
65+
values =("Geeta", "F", "25"))
66+
treev.insert("", 'end', text ="L8",
67+
values =("Ankit", "M", "22"))
68+
treev.insert("", 'end', text ="L10",
69+
values =("Mukul", "F", "25"))
70+
treev.insert("", 'end', text ="L11",
71+
values =("Mohit", "M", "16"))
72+
treev.insert("", 'end', text ="L12",
73+
values =("Vivek", "M", "22"))
74+
treev.insert("", 'end', text ="L13",
75+
values =("Suman", "F", "30"))
76+
77+
# Calling mainloop
78+
window.mainloop()

0 commit comments

Comments
 (0)