我有以下代码
import tkinter as tk
from tkinter import *
#------------------------------------------------------------------------------
class scrollPictures(Frame):
def __init__(self, parent, *args, **kw):
Frame.__init__(self, parent, *args, **kw)
vsb = Scrollbar(self, orient=VERTICAL)
vsb.pack(side=RIGHT, fill=Y, expand=False)
canvas = Canvas(self, borderwidth=0,
highlightthickness=0, yscrollcommand=vsb.set)
canvas.pack(side=LEFT, fill=BOTH, expand=True)
vsb.config(command = canvas.yview)
canvas.xview_moveto(0)
canvas.yview_moveto(0)
self.interior = interior = Frame(canvas)
interior_id = canvas.create_window(0,0, window=interior, anchor=NW)
def _configure_interior(event):
# update the scrollbars to match the size of the inner frame
size = (interior.winfo_reqwidth(), interior.winfo_reqheight())
print('in configure_interior %sx%s' % size)
canvas.config(scrollregion="0 0 %s %s" % size)
if interior.winfo_reqwidth() != canvas.winfo_width():
# update the canvas's width to fit the inner frame
canvas.config(width=interior.winfo_reqwidth())
interior.bind('<Configure>', _configure_interior)
def _configure_canvas(event):
print('in configure_canvas %s %s Canvas: %s %s' %
(interior.winfo_reqwidth(), interior.winfo_reqheight(),
canvas.winfo_width(), canvas.winfo_height()))
if interior.winfo_reqwidth() != canvas.winfo_width():
# update the inner frame's width to fill the canvas
print('Adjusting width')
canvas.itemconfigure(interior_id, width=canvas.winfo_width())
canvas.bind('<Configure>', _configure_canvas)
#------------------------------------------------------------------------------
print('Sandbox2')
def populate(frame):
for row in range(100):
tk.Label(frame, text="%s" % row, width=3, borderwidth="1",
relief="solid").grid(row=int(row/4), column=2*(row % 4), padx = 10)
t="this is the second column for row %s" %row
tk.Label(frame, text=t).grid(row=int(row/4), column=2*(row % 4) + 1, padx = 5)
if __name__ == "__main__":
root = tk.Tk()
screenWidth = root.winfo_screenwidth()
screenHeight = root.winfo_screenheight()
root.geometry('%dx%d+%d+%d' % (int(0.75 * screenWidth), int(0.75 * screenHeight), 100, 5))
root.title('Sandbox2')
fr = Frame(root)
scrollFrame = scrollPictures(root)
populate(scrollFrame.interior)
scrollFrame.grid(column = 0, row = 0, rowspan = 99, columnspan = 99)
root.mainloop()
这紧随Mikails的回应。只要scrollFrame具有根目录作为主目录,一切就可以正常工作。但是将root更改为fr,我没有任何显示。
此外,如何将滚动区域的高度设置得更大,例如屏幕的下半部分。刚接触tkinter时,它的一些细微差别使我难以理解。