我想輸出數據庫的內容到Tkinter小部件。該數據庫有足夠的行和列,我需要啓用水平和垂直滾動條,但我很難讓水平和垂直滾動同時工作。我並不想知道這Tkinter的部件使用,但這裏是我當前的實現:Tkinter小部件上的垂直和水平滾動條
# Create root
self.root = Tk()
self.root.geometry('1000x500+0+0')
# Create canvas
self.canvas = Canvas(self.root)
self.canvas.pack(side=TOP, fill=BOTH, expand=TRUE)
# Create scrollbars
self.xscrollbar = Scrollbar(self.root, orient=HORIZONTAL, command=self.canvas.xview)
self.xscrollbar.pack(side=BOTTOM, fill=X)
self.yscrollbar = Scrollbar(self.root, orient=VERTICAL, command=self.canvas.yview)
self.yscrollbar.pack(side=RIGHT, fill=Y)
# Attach canvas to scrollbars
self.canvas.configure(xscrollcommand=self.xscrollbar.set)
self.canvas.configure(yscrollcommand=self.yscrollbar.set)
# Create frame inside canvas
self.frame = Frame(self.canvas)
self.canvas.create_window((0,0), window=self.frame, anchor=NW)
self.frame.bind('<Configure>', self.set_scrollregion)
# Write db contents to canvas
self.print_dbcontents()
# Invoke main loop
self.root.mainloop()
def set_scrollregion(self, event):
self.canvas.configure(scrollregion=self.canvas.bbox('all'))
在此實現,我只能得到滾動在一個方向去努力,這取決於我如何收拾帆布:
self.canvas.pack(side=TOP, fill=BOTH, expand=TRUE) # scrolling works in x but not y
self.canvas.pack(side=LEFT, fill=BOTH, expand=TRUE) # scrolling works in y but not x
我只需要水平和垂直滾動同時工作,或找到一個替代解決方案。
本教程中的[滾動示例](http://www.tkdocs.com/tutorial/canvas.html#scrolling)按預期工作(導入應該修改爲Python 2.x)。 – jfs 2012-03-05 05:44:54