tkinter- 连接到文本小部件的滚动条可见,但没有任何滑块。



我正在构建一个需要显示大量文本的程序,我需要将滚动条附加到文本小部件上。

我正在使用Windows 7,python 3.3... 这是我正在使用的一个小(尽可能)的例子。我觉得我在这里错过了一些非常明显的东西,这让我**疯狂。

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)

我认为您的问题存在是因为您的代码存在两个问题。首先,您将文本小部件的高度设置为 500。该值表示字符而不是像素,因此将其设置为几千像素高。其次,您只插入一行文本,尽管长度为 40,000 个字符。如果将高度设置为更合理的值(例如 50 而不是 500),并在要插入的数据中插入换行符,则会看到滚动条开始正常运行。

在一个不相关的说明中,在__init__ViewAllEntriesInDatabaseWindow方法中调用self.grid()是完全无用的。您不能将顶级微件打包、放置或网格化到其他微件中。

最后,我建议你不要有任何类构造函数调用网格(或包,或位置)本身 - 这将使你的代码随着时间的推移难以维护。创建小部件时,父小部件应负责调用网格、包或放置。否则,如果您决定重新组织窗口,则必须编辑每个子小部件。

相关内容

最新更新