How To add horizontal and vertical scrollbar in Tkinter Widget - Python MagicStick Text Editor - Part 3

Posted by Suraj Singh on May 13, 2017 · 13 mins read
hii readers,




                                 In any Graphical Application, Scrollbars are always very useful for users and it is also very important Because with this widget we can easily scroll our page or panel up and down with the help of mouse in fastest way. So, In This Tutorials, I am going to show you how to add vertical and horizontal scrollbar in python tkinter Script or you can say how to use python tkinter scrollbar widget.

This Is our Third part of Python MagicStick Text Editor Project and In this post, i will show you how to add scrollbar feature easily in MagicStick python project.

But first, if you are a new visitor, Then I will suggest you to read our tutorials number wise. because in these tutorial series i am showing how to use python tkinter module to create a real life application.

Links For Tutorials

MagicStick Text Editor Introduction
MagicStick Text Editor Part 1
MagicStick Text Editor Part 2
MagicStick Text Editor Part 3
MagicStick Text Editor Part 4
MagicStick Text Editor Part 5
MagicStick Text Editor Part 6
MagicStick Text Editor Part 7
MagicStick Text Editor Part 8
MagicStick Text Editor Part 9
MagicStick Text Editor Part 10
MagicStick Text Editor Part 11
MagicStick Text Editor Part 12
MagicStick Text Editor Last Part

Or You Can Also Download Our Updated Example Codes Of Python MagicStick Text Editor Project For Understanding This Project More Clearly.

Check Here Our Github Repo




Now, let's focus to our main topic.

Here, First We will create a separated script "Scrollbar.py". And In this Script, We will Create a Scrollbar Class That can Add Scrollbar In Text Widget.


Check Below my Example Codes.


1. "Scrollbar.py"




 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
##
## ################################################
## ###### Please Don't Remove Author Name #########
## ############# Thanks ###########################
## ################################################
##
##
__author__='''

######################################################
By
######################################################

Suraj Singh


surajsinghbisht054@gmail.com
http://www.bitforestinfo.com/


######################################################
'''

from Graphics import Tkinter

class Scrollbar:
def __init__(self,text):
self.frame = text.master
self.text = text
self.text.configure(wrap='none')
self.for_x_view()
self.for_y_view()

def for_x_view(self):
# scroll Bar x For width
scroll_x=Tkinter.Scrollbar(self.frame, orient='horizontal',command=self.text.xview)
scroll_x.config(command=self.text.xview)
self.text.configure(xscrollcommand=scroll_x.set)
scroll_x.pack(side='bottom', fill='x', anchor='w')
return

def for_y_view(self):
# Scroll Bar y For Height
scroll_y = Tkinter.Scrollbar(self.frame)
scroll_y.config(command=self.text.yview)
self.text.configure(yscrollcommand=scroll_y.set)
scroll_y.pack(side='right', fill='y')
return


if __name__ == '__main__':
root = Tkinter.Tk()
pad = Tkinter.Text(root,wrap='none')
Scrollbar(pad)
pad.pack()
root.mainloop()

To Run These Script Codes alone, Just Change line 24 from

"from Graphics import Tkinter"

To  "import Tkinter"

Or

To Run This Codes as MagicStick Script. Don't Do Any Changes in this codes.

Now, let's join this script in MagicStick Text Editor Project.

To Connect This Script With MagicStick project, we need to do only two steps.

1. first, Place This Script In Our "MagicStick/magicsticklibs" Project Directory.
2. Add Function Of This Script in Our "ConfigSettings.py".



Let me explain you these steps in more details.

Step 1.
 Place "Scrollbar.py" In Our "MagicStick/magicsticklibs" Project Directory.


 MagicStick Project Directory Structure.


MagicStick_Editor_Part_2
|
|---> About.txt
|
|---> magicsticklibs
| |
| |---> __init__.py
| |
| |---> Main.py
| |
| |---> TextPad.py
| |
| |---> Graphics.py
| |
| |---> ConfigSettings.py
| |
| |---> ScrollBar.py
|
|
|---> main.py


Step 2.
          Add "Scrollbar.py" Function in Our "ConfigSettings.py" because in this project, "ConfigSettings.py" is responsible for joining libs in main stream.


ConfigSettings.py Modified Codes



 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
##
##
## ################################################
## ###### Please Don't Remove Author Name #########
## ############# Thanks ###########################
## ################################################
##
##
__author__='''

######################################################
By
######################################################

Suraj Singh


surajsinghbisht054@gmail.com
http://www.bitforestinfo.com/


######################################################
'''

from ScrollBar import Scrollbar

class Connect:
def __init__(self, pad):
self.pad = pad
self.modules_connections()


def modules_connections(self):
Scrollbar(self.pad)
return


Boom! Our Scrollbar.py script is now connected with MagicStick Project. To run this script in main stream of project. just run "main.py" script or you can also run this script alone and parallelled without touching project main stream just run "Scrollbar.py".


This Tutorial Ends Here,
In My Next Tutorial, We will Continue This project.


Written By