python - 如何使用 tkinter 在 python 中的现有框架内创建可滚动框架
问题描述
我想Sample Label 1, Sample Label 2, ...
在下面的代码中插入一个包含在一个框架内的可滚动框架。我试图通过遵循这个布局良好的网站来做到这一点。但是,尽管遵循这些说明并将它们调整为我的代码,我仍然看不到滚动条,即使我没有收到任何错误。
我怀疑这可能是pack
滚动条使其从主窗口中消失的方式。因此尝试使用place
within_
选项以将其相对于画布放置,但这会返回错误。提前感谢您的帮助。
import tkinter as tk
from tkinter import ttk
class MyGUI(tk.Frame):
def __init__(self, parent):
tk.Frame.__init__(self, parent)
# define variables
GRID = 25
X = GRID*20
Y = GRID*15
# create spinbox
self.Label = tk.Label(text='Number of Shafts')
self.Label.grid(row=0, column=0)
self.Spinbox = tk.Spinbox(from_=0, to=10, width=3)
self.Spinbox.grid(row=0, column=1)
# create scrollable frame
self.Table = tk.Canvas(height=100, width=150, background='white',
highlightthickness=1, highlightbackground='black')
self.Table.grid(row=1, column=0, columnspan=2)
self.Scrollbar = ttk.Scrollbar(self, orient='vertical', command=self.Table.yview)
self.Scrollbar.pack(side='right', fill='y')
self.Table.configure(yscrollcommand=self.Scrollbar.set)
self.Table.bind('<Configure>', lambda e: self.Table.configure(scrollregion=self.Table.bbox('all')))
self.Scrollable_Frame = ttk.Frame(self.Table)
self.Table.create_window((0, 0), window=self.Scrollable_Frame, anchor="nw")
self.Table.configure(yscrollcommand=self.Scrollbar.set)
for i in range(50):
ttk.Label(self.Scrollable_Frame, text=("Sample label %d" % i)).pack()
# create canvas
self.canvas = tk.Canvas(width=X+1, height=Y+1, background='white',
highlightthickness=1, highlightbackground='black')
self.canvas.grid(row=0, column=2, rowspan=2)
# create grid
for i in range(0, Y, GRID):
self.canvas.create_line(0, i, X, i, fill='gray')
for i in range(0, X, GRID):
self.canvas.create_line(i, 0, i, Y, fill='gray')
# create rectangle
x0 = GRID*3
y0 = GRID*4
wid = GRID
hei = GRID
self.create_shaft(x0, y0, wid, hei)
# add bindings to clicks
self._drag_data = {"x": 0, "y": 0, "item": None}
self.canvas.tag_bind('inertia', "<ButtonPress-1>", self.drag_start)
self.canvas.tag_bind('inertia', "<ButtonRelease-1>", self.drag_stop)
self.canvas.tag_bind('inertia', "<B1-Motion>",
lambda event, grid_x=GRID, grid_y=GRID: self.drag(event, grid_x, grid_y))
def create_shaft(self, x0, y0, wid, hei):
"""Create shaft drawing"""
self.canvas.create_rectangle(x0, y0, x0+wid, y0+hei, fill='gray', tags=('inertia',))
def drag_start(self, event):
"""Beginning drag of an object"""
# record the item and its location
self._drag_data["item"] = self.canvas.find_closest(event.x, event.y)[0]
self._drag_data["x"] = event.x
self._drag_data["y"] = event.y
def drag_stop(self, event):
"""End drag of an object"""
# reset the drag information
self._drag_data["item"] = None
self._drag_data["x"] = 0
self._drag_data["y"] = 0
def drag(self, event, grid_x, grid_y):
"""Handle dragging of an object"""
# compute how much the mouse has moved
delta_x = round((event.x - self._drag_data["x"])/grid_x)*grid_x
delta_y = round((event.y - self._drag_data["y"])/grid_y)*grid_y
# move the object the appropriate amount
self.canvas.move(self._drag_data["item"], delta_x, delta_y)
# record the new position
self._drag_data["x"] += delta_x
self._drag_data["y"] += delta_y
if __name__ == "__main__":
app = tk.Tk()
app.title('hi')
MyGUI(app).place()
app.mainloop()
解决方案
推荐阅读
- javascript - 由于不允许的 MIME 类型(“application/wasm”),加载模块被阻止
- python - 将数据添加到仅包含列名的现有空数据框
- android - 我应该如何知道为不同的视图添加什么依赖项?
- firebase - 强制 Firestore 数组与已经是数组一部分的值合并
- python - HexString 到打包的 EBCDIC 字符串
- html - 如何通过特定索引设置文本样式?
- python - 如何在python中将最多63位的数字转换为单词?
- haskell - Haskell:开头的函数解析错误
- swift - 当主类的超类为 UICollectionViewCell 时,如何从单元格执行Segue 或转到另一个视图
- java - List 和 LinkedList 之间的类型不兼容