Reputation: 69
I'm building a program that needs to display a large amount of text, and I need to have a scrollbar attached to a Text widget.
I'm using Windows 7 , python 3.3... Here's a small(est possible) example of what I'm working with. I feel like I'm missing something really obvious here and this is driving me **ing bonkers.
import datetime
import tkinter as tk
import tkinter.messagebox as tkm
import sqlite3 as lite
class EntriesDisplayArea(tk.Text):
"""
Display area for the ViewAllEntriesInDatabaseWindow
"""
def __init__(self,parent):
tk.Text.__init__(self, parent,
borderwidth = 3,
height = 500,
width = 85,
wrap = tk.WORD)
self.parent = parent
class EntriesDisplayFrame(tk.Frame):
"""
Containing frame for the text DisplayArea
"""
def __init__(self, parent):
tk.Frame.__init__(self, parent, relief = tk.SUNKEN,
width = 200,
borderwidth = 2)
self.parent = parent
self.grid(row = 0, column = 0)
self.entriesDisplayArea = EntriesDisplayArea(self)
self.entriesDisplayArea.grid(row = 1, column = 0, sticky = 'ns')
self.scrollVertical = tk.Scrollbar(self, orient = tk.VERTICAL,
command = self.entriesDisplayArea.yview)
self.entriesDisplayArea.config(yscrollcommand = self.scrollVertical.set)
for i in range(1000):
self.entriesDisplayArea.insert(tk.END,'asdfasdfasdfasdfasdfasdfasdfasdfasdfasdf')
self.scrollVertical.grid(row=1,column=1,sticky = 'ns')
class ViewAllEntriesInDatabaseWindow(tk.Toplevel):
"""
Window in which the user can view all of the entries entered ever
entered into the database.
"""
def __init__(self, parent = None):
tk.Toplevel.__init__(self,parent,
height = 400,
width = 400)
self.grid()
self.entriesDisplayFrame = EntriesDisplayFrame(self)
if __name__ == '__main__':
t0 = ViewAllEntriesInDatabaseWindow(None)
Upvotes: 1
Views: 204
Reputation: 385950
I think your problem exists because of two issues with your code. One, you're setting the height of the text widget to 500. That value represents characters rather than pixels, so you're setting it to a few thousand pixels tall. Second, you are only ever inserting a single line of text, albeit one that is 40,000 characters long. If you set the height to something more sane, such as 50 rather than 500, and insert line breaks in the data you're inserting, you'll see your scrollbar start to behave properly.
On an unrelated note, the call to self.grid()
in the __init__
method of ViewAllEntriesInDatabaseWindow
is completely useless. You can't pack, place or grid toplevel widgets into other widgets.
Finally, I recommend you do not have any class constructor call grid (or pack, or place) on itself -- this will make your code hard to maintain over time. When a widget is created, the parent widget should be responsible for calling grid, pack or place. Otherwise, if you decide to reorganize a window, you'll have to edit every child widget.
Upvotes: 2