2014-09-25 60 views
0

我大多複製了here中的代碼,它確實產生了一個用畫布和側面滾動條製作的新框架,但滾動條不起作用/未啓用。以下是創建新窗口的方法。在Tkinter中帶子窗口的滾動條

def ReviewTest(self, parsedFile = None): 
    if parsedFile is None: 
     return 
    else: 

     reviewTopLevel = Tk.Toplevel(self) 

     reviewTopLevel.geometry("%dx%d%+d%+d" % (600,400,0,0)) 

     verticalScroll = Tk.Scrollbar(reviewTopLevel, orient=Tk.VERTICAL) 
     verticalScroll.pack(fill=Tk.Y, side=Tk.RIGHT, expand=Tk.FALSE) 

     canvas = Tk.Canvas(reviewTopLevel, bd=0, highlightthickness=0, yscrollcommand=verticalScroll.set) 
     canvas.pack(side=Tk.LEFT, fill=Tk.BOTH, expand=Tk.TRUE) 
     verticalScroll.config(command=canvas.yview) 

     canvas.xview_moveto(0) 
     canvas.yview_moveto(0) 

     self.interior = interior = Tk.Frame(canvas) 
     interiorId = canvas.create_window(0,0, window=interior, anchor=Tk.NW) 

     reviewTopLevel.wm_title("Review Test") 


     def _configure_interior(event): 
      size = (reviewTopLevel.winfo_reqwidth(), reviewTopLevel.winfo_reqheight()) 
      canvas.configure(scrollregion="0 0 %s %s" % size) 
      if reviewTopLevel.winfo_reqwidth() != canvas.winfo_width(): 
       canvas.configure(width=reviewTopLevel.winfo_width()) 

     interior.bind('<Configure>', _configure_interior) 

     def _configure_canvas(event): 
      if reviewTopLevel.winfo_reqwidth() != canvas.winfo_reqwidth(): 
       canvas.configure(width=reviewTopLevel.winfo_reqwidth()) 

     canvas.bind('<Configure>', _configure_canvas) 

     self.canvasWidgets = [] 

     for keyIndex, key in enumerate(parsedFile.GetKeys()): 
      category = Tk.Label(canvas, text=key, anchor=Tk.W) 
      category.grid(row = 0, column = keyIndex, sticky=Tk.EW) 
      self.canvasWidgets.append(category) 

      currentColumnKey = parsedFile.GetColumns()[key] 

      for dataIndex in range(0, len(currentColumnKey)): 
       data = Tk.Entry(canvas) 
       data.insert(0, currentColumnKey[dataIndex]) 
       data.grid(row = dataIndex + 1, column = keyIndex, sticky=Tk.EW) 
       self.canvasWidgets.append(data) 

回答

1

我沒有仔細閱讀其他帖子,添加的小部件應該設置爲內部,而不是畫布。

self.canvasWidgets = [] 

     for keyIndex, key in enumerate(parsedFile.GetKeys()): 
      category = Tk.Label(interior, text=key, anchor=Tk.W) 
      category.grid(row = 0, column = keyIndex, sticky=Tk.EW) 
      self.canvasWidgets.append(category) 

      currentColumnKey = parsedFile.GetColumns()[key] 

      for dataIndex in range(0, len(currentColumnKey)): 
       data = Tk.Entry(interior) 
       data.insert(0, currentColumnKey[dataIndex]) 
       data.grid(row = dataIndex + 1, column = keyIndex, sticky=Tk.EW) 
       self.canvasWidgets.append(data)