473,466 Members | 1,382 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Adding scroll bar to text box in Tkinter Python

1 New Member
Hi Guys,

I'm new to Python. And I'm preparing a GUI in Python and I need to add scroll bar to the Text box. And I'm using 'Tkinter GRID' manager for this. Attached is the image of text box what I need.
I'm getting very large text box if I add a Scroll Bar into it. Below is the code what I have tried,
Please any body help me.
Expand|Select|Wrap|Line Numbers
  1. TextBox1 = Entry(resultFrame)
  2. datFileText.grid(row=2, column=0, padx=20, ipadx=200, ipady=40)
  3. scrollBar1 = ScrolledText(TextBox1 )
  4. scrollBar1.grid(sticky=E)

Thanks in advance
Manoj
Attached Images
File Type: jpg TextBox_With_ScrollBar.jpg (6.3 KB, 1687 views)
Feb 7 '12 #1
2 9305
bvdet
2,851 Recognized Expert Moderator Specialist
You don't show what datFileText is in the code you posted. Below is a Tkinter application with a Tkinter.Text widget and scrollbars. The ScrolledText module in Python 2.7 uses the same constructor as Tkinter.Text.

A Tkinter.Entry widget can only be scrolled horizontally. It takes some extra work to setup. The New Mexico Tech Tkinter 8.4 reference can show you how.

Expand|Select|Wrap|Line Numbers
  1. import Tkinter
  2. """
  3. Edit a file and save the text.
  4. """
  5.  
  6. textFont1 = ("Courier New", 16, "normal")
  7.  
  8. class ScrollbarX(Tkinter.Scrollbar):
  9.     def set(self, low, high):
  10.         if float(low) <= 0.0 and float(high) >= 1.0:
  11.             self.grid_remove()
  12.         else:
  13.             self.grid()
  14.         Tkinter.Scrollbar.set(self, low, high)
  15.  
  16. class App(Tkinter.Tk):
  17.     def __init__(self, fn, fnout):
  18.         Tkinter.Tk.__init__(self)
  19.         self.title("Text Widget")
  20.         self.fin = open(fn, 'r')
  21.         self.fnout = fnout
  22.         self.mainFrame = Tkinter.Frame(self)
  23.  
  24.         top=self.winfo_toplevel()
  25.         top.columnconfigure(0, weight=1)
  26.         top.rowconfigure(0, weight=1)
  27.  
  28.         self.mainFrame.grid(row=0, column=0, sticky="nsew")
  29.         self.exit = Tkinter.Button(self.mainFrame,
  30.                                    text="Save and Exit",
  31.                                    command=self.finish)
  32.         self.exit.grid(row=0, column=0, sticky="ns")
  33.  
  34.  
  35.         self.mainFrame.columnconfigure(0, weight=1)
  36.         self.mainFrame.rowconfigure(1, weight=1)
  37.  
  38.         vscrollbar = ScrollbarX(self.mainFrame)
  39.         vscrollbar.grid(row=1, column=1, sticky="ns")
  40.         hscrollbar = ScrollbarX(self.mainFrame, orient=Tkinter.HORIZONTAL)
  41.         hscrollbar.grid(row=2, column=0, sticky="ew")
  42.  
  43.         self.textWidget = Tkinter.Text(self.mainFrame,
  44.                                        yscrollcommand=vscrollbar.set,
  45.                                        xscrollcommand=hscrollbar.set,
  46.                                        wrap=Tkinter.NONE,
  47.                                        height=24,
  48.                                        width=60,
  49.                                        font=textFont1)
  50.         self.textWidget.insert("1.0", self.fin.read())
  51.         self.textWidget.grid(row=1, column=0, sticky="nsew")
  52.  
  53.         hscrollbar["command"] = self.textWidget.xview
  54.         vscrollbar["command"] = self.textWidget.yview
  55.  
  56.     def finish(self):
  57.         fout = open(self.fnout, 'w')
  58.         fout.write(self.textWidget.get("1.0", "end"))
  59.         fout.close()
  60.         self.fin.close()
  61.         app.destroy()
  62.  
  63. if __name__ == "__main__":
  64.     fn = "edit.txt"
  65.     fnout = "editresult.txt"
  66.     app = App(fn, fnout)
  67.     app.mainloop()
Feb 7 '12 #2
dwblas
626 Recognized Expert Contributor
It's much easier if you add the Pmw extension and use the ScrolledText widget from Pmw. http://www.ittc.ku.edu/~niehaus/clas.../scrolltext.py
Feb 10 '12 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Andrei | last post by:
I've installed Python 2.3b2 on Windows XP (in "e:\programming\Python2.3"). I also have an older ActivePython 2.2.2 distro on my C-drive and Ruby which apparently installs tcl as well. Now I'm...
1
by: vishnu mahendra | last post by:
hello to all, I have do create a simple editor.so Please, any one tell me how to scroll text. happy halloween to all. thank you in advance, vishnu.
5
by: marijuanated | last post by:
Hi all, I am wondering if i could change a button text dynamically in its handler. for example,can we do something like this: curButton.bind("<Button-1>",self.StopServer) def...
0
by: nevada17 | last post by:
My goal is to find a way to apply scroll bars across the y axis of about 25 check buttons so that my interface isn't too large. I've read about scroll bars and I realize that's its not possible to...
2
by: prashantkisanpatil | last post by:
Hi All, I am trying to wrap a japanese text in Python, by the following code. if len(message) 54: message = message.decode("UTF8") strlist = textwrap.wrap(message,54) After this I am...
5
by: Schwanitz | last post by:
Hello, how can I scroll text in a textfield using the wheel of the mouse in a form with VB? At the moment the form is going to the next record. Regards and thank you!
5
vdraceil
by: vdraceil | last post by:
Is it possible in vb6.0 to add scroll bars to a form,so that its length and width are increased?
0
by: gobblegob | last post by:
Well i cannont remember where i picked this code up, but i have seen it asked for in many forums, Many times so i thought i'd add it here (Since this is my favourite forum). Auto scroll text in...
2
by: yorlina007 | last post by:
Hi I need to design a tkinter widget where i should have a browse option button as you see in all windows application to select a file from my machine. Also i need to select an integer from...
0
by: sarfarazfarru | last post by:
sir as am very new to the MFC just started working. i am facing difficulties to scroll the window which has fixed text. i am not able to scroll the text both way horizontal and vertical please any...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.