如何将滚动条添加到框架?

时间:2019-04-28 15:01:16

标签: python tkinter

我正在使用tkinter和Python 2.7制作COCOMO 2 Simulator。我必须从用户那里获得许多成本和规模因素的价值。为此,我正在使用单选按钮。由于因素很多,因此我的相框无法显示所有内容。

为此,我尝试使用此answer的帮助将滚动条添加到框架中。我必须设法在屏幕上获得滚动条,但没有任何动作。

import Tkinter as tk
import ttk

LARGE_FONT = ("Verdana", 40)
BUTTON_FONT = ("Verdana", 20)
RADIO_FONT = ('Verdana', 15)


class GUI(tk.Tk):

    def __init__(self, *args, **kwargs):

        tk.Tk.__init__(self, *args, **kwargs)
        container = tk.Frame(self)

        container.pack(side="top", fill="both", expand = True)

        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)


        frame = Effort_Page(container, self)
        frame.grid(row=0, column=0, sticky="nsew")
        frame.tkraise()


class Effort_Page(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        vscrollbar = tk.Scrollbar(self, orient=tk.VERTICAL)
        vscrollbar.pack(fill=tk.Y, side=tk.RIGHT, expand=tk.FALSE)
        canvas = tk.Canvas(self, bd=0, highlightthickness=0,
                        yscrollcommand=vscrollbar.set)
        canvas.pack(side=tk.LEFT, fill=tk.BOTH, expand=tk.TRUE)
        vscrollbar.config(command=canvas.yview)
        canvas.xview_moveto(0)
        canvas.yview_moveto(0)
        self.interior = interior = tk.Frame(canvas)
        interior_id = canvas.create_window(0, 0, window=interior,
                                           anchor="nw")
        self.set_costDrivers()
        labelH = tk.Label(self, text="Effort Calculator", font=LARGE_FONT)
        labelH.place(x=650, y=30)
        scale_driver_heading = tk.Label(self, text="Scale Drivers", font=BUTTON_FONT)
        scale_driver_heading.place(x=70, y=230)

        rc_vl = []
        v1 = [0, 1 , 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]
        h = 0

        for i in range(5):

            rc_vl.append(tk.Radiobutton(self, 
                          text="VL", 
                          variable=v1[i], 
                          value=self.costDrivers["VL"][i] ))

            rc_vl[i].place(x=650, y=700+h)
            rc_vl[i].config(font=RADIO_FONT)
            h = h + 100

        def _configure_interior(event):
            # update the scrollbars to match the size of the inner frame
            size = (interior.winfo_reqwidth(), interior.winfo_reqheight())
            canvas.config(scrollregion="0 0 %s %s" % size)
            if interior.winfo_reqwidth() != canvas.winfo_width():
                # update the canvas's width to fit the inner frame
                canvas.config(width=interior.winfo_reqwidth())
        interior.bind('<Configure>', _configure_interior)

        def _configure_canvas(event):
            if interior.winfo_reqwidth() != canvas.winfo_width():
                # update the inner frame's width to fill the canvas
                canvas.itemconfigure(interior_id, width=canvas.winfo_width())
        canvas.bind('<Configure>', _configure_canvas)

    def onFrameConfigure(self, event):
        '''Reset the scroll region to encompass the inner frame'''
        self.canvas.configure(scrollregion=self.canvas.bbox("all"))

    def set_costDrivers(self):

        self.costDrivers = dict()
        self.costDrivers["VL"] = [0.75, 0, 0.75, 0, 0.89, 0, 0, 0, 1.50, 1.37, 1.24, 1.22,
                                1.25, 1.22, 1.24, 1.25, 1.29]

        self.costDrivers["L"] = [0.88, 0.93, 0.88, 0.91, 0.95, 0, 0, 0.87, 1.22, 1.16, 1.10,
                                1.10, 1.12, 1.10, 1.12, 1.10, 1.10]

        self.costDrivers["N"] = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]

        self.costDrivers["H"] = [1.15, 1.09, 1.15, 1.14, 1.06, 1.11, 1.06, 1.15, 0.83, 0.87,
                                0.92, 0.89, 0.88, 0.91, 0.86, 0.92, 1]

        self.costDrivers["VH"] = [1.39, 1.19, 1.30, 1.29, 1.13, 1.31, 1.21, 1.30, 0.67,
                                0.74, 0.84, 0.81, 0.81, 0.84, 0.72, 0.84, 1] 

        self.costDrivers["EH"] = [0, 0, 1.66, 1.49, 0, 1.67, 1.57, 0, 0, 0, 0, 0, 0, 0, 0,
                                0.78, 0]        


app = GUI()
app.geometry("2000x1600")
app.mainloop()

1 个答案:

答案 0 :(得分:0)

您发布的代码由于几种不同的原因而无法运行,因此无法复制您的问题。

但是,很明显,您没有将单选按钮放在画布内的框架内,所以这就是为什么它们不会滚动的原因。您需要将它们放在interior框架内。

相关问题