如何在框架中添加滚动条?

2024-03-29 02:02:12 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用tkinter和python2.7来制作cocomo2模拟器。我必须从用户那里获得许多成本和规模因素的价值。为此,我使用单选按钮。由于有许多因素,我的框架无法显示所有内容。你知道吗

为此,我尝试将scrollbar添加到hep为thisanswer的框架中。我不得不设法在屏幕上得到一个滚动条,但什么也不动。你知道吗

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()

Tags: theselfinitconfigurecontainerdefwidthframe
1条回答
网友
1楼 · 发布于 2024-03-29 02:02:12

由于几个不同的原因,您发布的代码无法运行,因此不可能复制您的问题。你知道吗

但是,很明显,您没有将单选按钮放在画布内的框架内,因此它们不会滚动。你需要把它们放在interior框架内。你知道吗

相关问题 更多 >