Tkinter内容和滚动条未正确显示

时间:2016-02-07 13:51:36

标签: python-2.7 tkinter tkinter-canvas

此代码由带有滚动条的输入框组成,滚动条应显示在最右侧。但是,框不可见,滚动条也不在正确的位置。我想这是在画布中传递/创建帧的问题,但似乎没有任何效果。我将不胜感激任何帮助。

    from Tkinter import *
    from random import randint

    root = Tk()
    root.geometry("{0}x{1}+0+0".format(root.winfo_screenwidth(),root.winfo_screenheight()))

    global label2

    def onFrameConfigure(canvas):
        canvas.configure(scrollregion=canvas.bbox("all"))

    def populate(label2):
        Label(label2, bg='Light Blue',font = ("Verdana 10 bold"),text="NUMBER OF COMPONENTS REQUIRED").grid(row = 1, column = 1, sticky = W, padx = 10)
        Label(label2, bg='Light Blue',text="COMPONENT 1").grid(row = 4, column = 1, padx = 10,sticky=W)
        Label(label2, bg='Light Blue',text="COMPONENT 2").grid(row = 5, column = 1, padx = 10, sticky=W)
        Label(label2, bg='Light Blue',text="COMPONENT 3").grid(row = 6, column = 1, padx = 10, sticky=W)
        Label(label2, bg='Light Blue',text="COMPONENT 4").grid(row = 7, column = 1, padx = 10,sticky=W)
        Label(label2, bg='Light Blue',text="COMPONENT 5").grid(row = 8, column = 1, padx = 10,sticky=W)
        Label(label2, bg='Light Blue',text="COMPONENT 6").grid(row = 9, column = 1, padx = 10,sticky=W)
        Label(label2, bg='Light Blue',text="COMPONENT 7").grid(row = 10, column = 1, padx = 10,sticky=W)
        Label(label2, bg='Light Blue',text="COMPONENT 8").grid(row = 11, column = 1, padx = 10,sticky=W)
        Label(label2, bg='Light Blue',text="COMPONENT 9").grid(row=12, column = 1, padx = 10,sticky=W)
        Label(label2, bg='Light Blue',text="COMPONENT 10").grid(row = 13, column = 1, padx = 10,sticky=W)
        Label(label2, bg='Light Blue',text="COMPONENT 11").grid(row = 14, column = 1, padx = 10,sticky=W)
        Label(label2, bg='Light Blue',text="COMPONENT 12").grid(row=15, column = 1, padx = 10,sticky=W)
        Label(label2, bg='Light Blue',text="COMPONENT 13").grid(row = 16, column = 1, padx = 10,sticky=W)
        Label(label2, bg='Light Blue',text="COMPONENT 14").grid(row = 17, column = 1, padx = 10,sticky=W)
        Label(label2, bg='Light Blue',text="COMPONENT 15").grid(row = 18, column = 1, padx = 10,sticky=W)
        Label(label2, bg='Light Blue',text="COMPONENT 16").grid(row = 19, column = 1, padx = 10,sticky=W)
        Label(label2, bg='Light Blue',text="COMPONENT 17").grid(row = 20, column = 1, padx = 10,sticky=W)
        Label(label2, bg='Light Blue',text="COMPONENT 18").grid(row = 22, column = 1, padx = 10,sticky=W)

        a1 = Entry(label2, width = 10)
        a2 = Entry(label2, width = 10)
        a3 = Entry(label2, width = 10)
        a4 = Entry(label2, width = 10)
        a5 = Entry(label2, width = 10)
        a6 = Entry(label2, width = 10)
        a7 = Entry(label2, width = 10)
        a8 = Entry(label2, width = 10)
        a9 = Entry(label2, width = 10)
        a10 = Entry(label2, width = 10)
        a11 = Entry(label2, width = 10)
        a12 = Entry(label2, width = 10)
        a13 = Entry(label2, width = 10)
        a14 = Entry(label2, width = 10)
        a15 = Entry(label2, width = 10)
        a16 = Entry(label2, width = 10)
        a17 = Entry(label2, width = 10)
        a18 = Entry(label2, width = 10)
        a19 = Entry(label2, width = 10)
        a20 = Entry(label2, width = 10)
        a21 = Entry(label2, width = 10)
        a22 = Entry(label2, width = 10)
        a23 = Entry(label2, width = 10)

        a1.grid(row = 4, column = 2, pady = 10)
        a2.grid(row = 5, column = 2, pady = 10)
        a3.grid(row = 6, column = 2, pady = 10)
        a4.grid(row = 7, column = 2, pady = 10)
        a5.grid(row = 8, column = 2, pady = 10)
        a6.grid(row = 9, column = 2, pady = 10)
        a7.grid(row = 10, column = 2, pady = 10)
        a8.grid(row = 11, column = 2, pady = 10)
        a9.grid(row = 12, column = 2, pady = 10)
        a10.grid(row = 13, column = 2, pady = 10)
        a11.grid(row = 14, column = 2, pady = 10)
        a12.grid(row = 15, column = 2, pady = 10)
        a13.grid(row = 16, column = 2, pady = 10, padx = 10)
        a14.grid(row = 17, column = 2, pady = 10)
        a15.grid(row = 18, column = 2, pady = 10)
        a16.grid(row = 19, column = 2, pady = 10)
        a17.grid(row = 20, column = 2, pady = 10, padx = 10)
        a18.grid(row = 20, column = 3, pady = 10, padx = 10)
        a19.grid(row = 20, column = 4, pady = 10, padx = 10)
        a20.grid(row = 21, column = 2, pady = 10, padx = 10)
        a21.grid(row = 21, column = 3, pady = 10, padx = 10)
        a22.grid(row = 21, column = 4, pady = 10, padx = 10)
        a23.grid(row = 22, column = 2, pady = 10, padx = 10)

        Button(label2, text='Save', command=root.quit).grid(row = 23, column=4, sticky=W, padx = 20, pady = 10)
        Button(label2, text='Reset', command=root.quit).grid(row = 24, column=5, sticky=W)

   canvas = Canvas(root)
   frame = Frame(canvas)
   label2 = Frame(root, bg = 'Light Blue')
   vsb = Scrollbar(canvas, orient = "vertical", command = canvas.yview)
   canvas.config(yscrollcommand = vsb.set, scrollregion = canvas.bbox(ALL))
   print canvas.config('scrollregion')
   vsb.pack(side = "right", fill = "y", anchor = E)
   vsb.grid(sticky = NE)
   canvas.pack(side = "right", fill = "both", expand = True)
   canvas.grid(sticky = NE+SE)
   canvas.create_window((4,4), window = label2, anchor = "ne")

   frame.bind("<Configure>", lambda event, canvas = canvas: onFrameConfigure(canvas))

   populate(frame)

   root.mainloop( )

1 个答案:

答案 0 :(得分:0)

您已将label2作为root的孩子,但随后您在画布中嵌入了label2。但是,您的populate方法会将小部件放入frame