正如你在圖片中看到的,我希望畫布滾動條一直向右走……簡而言之,我希望畫布覆寫所有的紅色背景frame 2……有什么想法嗎?這是代碼:(請參考下面有關您將在
如果你們能告訴我如何用滑鼠滾輪移動滾動條也會很有幫助
uj5u.com熱心網友回復:
下次請添加一個完整的可重現示例,包括匯入和主回圈,以便更容易理解。
使您的畫布可滾動
(來源:tkinter:將滑鼠滾輪系結到滾動條):
# bind scrolling to mousewheel
def _scroll_canvas(event):
canvas.yview_scroll(-1*(int(event.delta/100)), "units")
canvas.bind_all("<MouseWheel>", _scroll_canvas)
將滾動條貼在右側
- 您不需要僅用于 Canvas 的另一個框架,我移除了您的
containerFrame 并將 Canvas 直接放入您的midFrame. 紅色背景也可以洗掉。 - 您的滾動條也是如此 - 直接放入您的 midFrame
- 對于紅色部分,我創建了一個名為 red_frame 的新子框架(在底部)
- 如果您希望滾動條出現在螢屏的右側,請使用帶有“right”選項的 pack,對于所有其他子框架,請使用“left”選項。其他一切都由您打包小部件的順序管理。
完整代碼:
from tkinter import Frame, Canvas, Scrollbar, Tk, Label
window = Tk()
upFrame = Frame(window, bg='lightgray')
upFrame.grid(row=1, column=0, sticky='nesw')
midFrame = Frame(window, bg='red')
midFrame.grid(row=2, column=0, sticky='nesw')
bottomFrame = Frame(window, bg='lightgray')
bottomFrame.grid(row=3, column=0, sticky='nesw')
window.grid_rowconfigure(1, weight = 0)
window.grid_columnconfigure(0, weight = 1)
window.grid_rowconfigure(1, weight = 0)
canvas = Canvas(midFrame)
scrollbar = Scrollbar(midFrame, orient="vertical", command=canvas.yview)
scrollable_frame = Frame(canvas)
scrollable_frame.bind(
"<Configure>",
lambda e: canvas.configure(
scrollregion=canvas.bbox("all")
)
)
canvas.create_window((0, 0), window=scrollable_frame, anchor="nw")
canvas.configure(yscrollcommand=scrollbar.set)
# bind scrolling to mousewheel
def _scroll_canvas(event):
canvas.yview_scroll(-1*(int(event.delta/100)), "units")
canvas.bind_all("<MouseWheel>", _scroll_canvas)
for i in range(50):
Label(scrollable_frame, text="Sample scrolling label").pack()
# here you decide in which order your widgets appear: canvas, red frame and on the right - your scrollbar
canvas.pack(side='left', fill='both')
red_frame = Frame(midFrame, bg="red")
red_frame.pack(side="left")
scrollbar.pack(side="right", fill="y")
window.mainloop()
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/393612.html
標籤:Python 特金特 滚动条 tkinter-canvas
上一篇:tkinter番茄鐘的暫停和恢復
