2015-11-16 29 views
0

我正在一个程序中有一个可滚动的框架,将包含大量的项目。但与我的应用程序,它并没有呈现所有的人。有人可能会告诉我为什么?我该如何解决它?可滚动的框架不会渲染它中的所有项Python Tkinter

代码:

#700x650 

from Tkinter import * 
import ttk 

class itemLoad: 
    def __init__(self): 
     pass 

    def item(self): 
     items = "Video File,Image File,None,King King" 

     return items 

class App(Frame): 
    def __init__(self, parent): 
     Frame.__init__(self, parent) 
     self.pack(fill=BOTH) 

     self.loadItem = itemLoad() 

     self.one = None 

     self.create_widgets() 
     self.loadItems() 

    def create_widgets(self): 
     self.mainFrame = Frame(self, width=700, height=650) 
     self.mainFrame.pack_propagate(False) 
     self.mainFrame.pack() 

     self.menu = Frame(self.mainFrame, width=150, height=650, bg="Gray92") 
     self.menu.pack_propagate(False) 
     self.menu.pack(side=LEFT) 

     self.itemMenu = Frame(self.mainFrame, width=550, height=650) 
     self.itemMenu.pack_propagate(False) 
     self.itemMenu.pack(side=LEFT) 

     self.vScroller = ttk.Scrollbar(self.itemMenu, orient=VERTICAL) 
     self.vScroller.pack(side=RIGHT, fill=Y) 

     self.canvas = Canvas(self.itemMenu, bd=0, width=534, highlightthickness=0, yscrollcommand=self.vScroller.set) 
     self.canvas.pack_propagate(False) 
     self.canvas.pack(side=LEFT, fill=BOTH) 
     self.vScroller.config(command=self.canvas.yview) 

     self.innerFrame = Frame(self.canvas, width=550, height=650, bg="Pink") 
     self.canvas.create_window(0, 0, window=self.innerFrame, anchor=NW) 

     def update(event): 
      self.canvas.config(scrollregion=self.canvas.bbox("all")) 

     self.innerFrame.bind("<Configure>", update) 

     self.spacer = Frame(self.mainFrame, bg="Gray") 
     self.spacer.pack(side=LEFT, fill=Y) 

     frame = Frame(self.menu, bg="Gray92") 
     frame.pack(side=TOP, fill=X) 

     high = Frame(frame, bg="Gray92", width=10) 
     high.pack(side=LEFT, fill=Y) 

     self.bu1 = Label(frame, font=("Calibri", 14), text=" Main Folder", width=12, anchor=W, bg="Gray92") 
     self.bu1.pack(side=LEFT, fill=X, ipadx=10, ipady=10) 

     frame2 = Frame(self.menu, bg="Gray92") 
     frame2.pack(side=TOP, fill=X) 

     high2 = Frame(frame2, bg="Gray92", width=10) 
     high2.pack(side=LEFT, fill=Y) 

     self.bu2 = Label(frame2, font=("Calibri", 14), text=" Favorited", width=12, anchor=W, bg="Gray92") 
     self.bu2.pack(side=LEFT, fill=X, ipadx=10, ipady=10) 

     frame3 = Frame(self.menu, bg="Gray92") 
     frame3.pack(side=TOP, fill=X) 

     high3 = Frame(frame3, bg="Gray92", width=10) 
     high3.pack(side=LEFT, fill=Y) 

     self.bu3 = Label(frame3, font=("Calibri", 14), text=" Trash Can", width=12, anchor=W, bg="Gray92") 
     self.bu3.pack(side=LEFT, fill=X, ipadx=10, ipady=10) 

     frame4 = Frame(self.menu, bg="Gray92") 
     frame4.pack(side=BOTTOM, fill=X) 

     high4 = Frame(frame4, bg="Gray92", width=10) 
     high4.pack(side=LEFT, fill=Y) 

     self.bu4 = Label(frame4, font=("Calibri", 14), text=" Log Out", width=12, anchor=W, bg="Gray92") 
     self.bu4.pack(side=LEFT, fill=X, ipadx=10, ipady=10) 

     def hover(event): 
      widg = event.widget 

      items = widg.winfo_children() 

      if items[1].cget("text") == self.one: 
       pass 
      else: 
       items[0].config(bg="Gray85") 
      items[1].config(bg="Gray85") 

     def unHover(event): 
      widg = event.widget 

      text = None 

      items = widg.winfo_children() 

      if items[1].cget("text") == self.one: 
       pass 
      else: 
       items[0].config(bg="Gray92") 
      items[1].config(bg="Gray92") 

     def clicked(event): 
      widg = event.widget 
      par = widg.winfo_parent() 
      par = self.menu._nametowidget(par) 

      for item in self.menu.winfo_children(): 
       items = item.winfo_children() 

       items[0].config(bg="Gray92") 

      for item in par.winfo_children(): 
       try: 
        self.one = item.cget("text") 
       except: 
        item.config(bg="lightBlue") 

     frame.bind("<Enter>", hover) 
     frame2.bind("<Enter>", hover) 
     frame3.bind("<Enter>", hover) 
     frame4.bind("<Enter>", hover) 
     frame.bind("<Leave>", unHover) 
     frame2.bind("<Leave>", unHover) 
     frame3.bind("<Leave>", unHover) 
     frame4.bind("<Leave>", unHover) 
     high.bind("<Button-1>", clicked) 
     self.bu1.bind("<Button-1>", clicked) 
     high2.bind("<Button-1>", clicked) 
     self.bu2.bind("<Button-1>", clicked) 
     high3.bind("<Button-1>", clicked) 
     self.bu3.bind("<Button-1>", clicked) 
     high4.bind("<Button-1>", clicked) 
     self.bu4.bind("<Button-1>", clicked) 

    def loadItems(self): 
     theItems = self.loadItem.item() 

     for i in range(0, 500): 
      none = Frame(self.innerFrame, width=200, height=500, bg="red") 
      none.pack_propagate(False) 
      none.pack(side=TOP, padx=10, pady=10) 

      let = Label(none, text=i) 
      let.pack(side=TOP) 

root = Tk() 
root.geometry("700x650") 
root.resizable(0,0) 

app = App(root) 

root.mainloop() 

回答

1

我想你超过了Tkinter的画布的限制。您尝试滚动的框架高度为250,000像素。我怀疑画布可以处理。

当我使所有的内部小部件相当小,你的代码工作正常。

+0

是的,我让它们变得更小,我确实获得了更多的小部件,但我主要是测试以查看它的极限。有没有办法通过这个限制? –

+0

@UnknownCode:看到这个问题:http://stackoverflow.com/q/26578213/7432 –