1 回答

TA貢獻(xiàn)1862條經(jīng)驗(yàn) 獲得超6個(gè)贊
您可以定義自己的yview()方法,僅當(dāng)畫布的全部?jī)?nèi)容不可見(jiàn)時(shí)才進(jìn)行滾動(dòng)。為此,我使用了,如果所有畫布內(nèi)容都可見(jiàn),self.canvas.yview()則返回。(0.0, 1.0)然后我使用自定義yview()方法作為滾動(dòng)條命令和鼠標(biāo)滾輪綁定。
import tkinter as tk
class Gui:
def __init__(self, master):
self.root = master
self.canvas = tk.Canvas(self.root)
self.frame = tk.Frame(self.canvas,bg='white')
self.scroll = tk.Scrollbar(self.canvas,orient='vertical', command=self.yview)
self.canvas.configure(yscrollcommand=self.scroll.set)
self.scroll.pack(side='right', fill='y')
self.canvas.place(relheight=1, relwidth=0.85, relx=0.15)
self.canvas_frame = self.canvas.create_window((0,0), window=self.frame, anchor='nw')
self.frame.bind('<Configure>', self.onFrameConfigure)
self.canvas.bind_all('<MouseWheel>', lambda event: self.yview('scroll', int(-1*(event.delta/120)), 'units'))
self.addToFrame()
def onFrameConfigure(self, event):
self.canvas.configure(scrollregion=self.canvas.bbox('all'))
def addToFrame(self):
self.label = tk.Label(self.frame,text='Label')
self.label.pack()
def yview(self, *args):
if self.canvas.yview() == (0.0, 1.0):
return
self.canvas.yview(*args)
if __name__ == "__main__":
root = tk.Tk()
gui = Gui(root)
root.mainloop()
添加回答
舉報(bào)