我无法为自己的生活弄清楚为什么这不起作用?我只是想让一个画布与滚动条一起滚动。
对于问题Tkinter Scrollbar not working,我一直遵循@BryanOakley的建议,但似乎无法弄清楚自己在做什么错。
这是我的代码:
import tkinter as tk
class Application(tk.Frame):
def __init__(self, master=None):
super().__init__(master, bg= "#E3E5E6")
self.master = master
self.grid(sticky = "nesw")
self.grid_rowconfigure(1, weight=1)
self.grid_columnconfigure(0, weight=1)
self.canvas = tk.Canvas(master)
self.canvas.create_oval(10, 10, 20, 20, fill="red")
self.canvas.create_oval(200, 200, 220, 220, fill="blue")
self.canvas.grid(row=0, column=0, sticky = "nesw")
self.scroll_x = tk.Scrollbar(master, orient="horizontal", command=self.canvas.xview)
self.scroll_x.grid(row=1, column=0, sticky="ew")
self.scroll_y = tk.Scrollbar(master, orient="vertical", command=self.canvas.yview)
self.scroll_y.grid(row=0, column=1, sticky="ns")
self.canvas.configure(yscrollcommand=self.scroll_y.set, xscrollcommand=self.scroll_x.set)
self.canvas.configure(scrollregion=self.canvas.bbox((0,0,15000,15000)))
if __name__ == "__main__":
root = tk.Tk()
root.grid_rowconfigure(0, weight=1)
root.grid_columnconfigure(0, weight=1)
root.geometry("{}x{}+0+0".format(600,400))
app = Application(master=root)
app.mainloop()
非常感谢您的帮助。
答案 0 :(得分:1)
更改此:
self.canvas.configure(scrollregion=self.canvas.bbox((0,0,15000,15000)))
对此:
self.canvas.configure(scrollregion=(0,0,15000,15000))
scrollregion
属性需要四个坐标的元组。调用bbox
可以返回该元组,但前提是您给它提供了项目ID或标签。您正在为它提供一个元组,并且由于画布上没有任何项目具有类似于该元组的标签,因此它将返回None
。