0

关于我的程序,我有点卡在这个问题上。我尝试添加尽可能多的注释,以了解代码中的所有内容,但本质上。该程序有一个字段和值输入框。单击“添加字段/值按钮”时,会添加更多条目小部件。如果这种情况继续发生,那么显然它会离开屏幕。所以我限制了应用程序的大小,但问题是我需要一个滚动条。我试过查找它,但是我的框架使用网格,并且在任何地方他们都使用在这种情况下不兼容的包。我让滚动条出现,但它似乎不起作用。我见过有些人使用画布,以及不止一个框架等。我错过了一些重要的东西,但我不知道如何用网格做完全相同的事情。

from tkinter import *
import tkinter as tk

class Insert(tk.Tk):

    def __init__(self):
        tk.Tk.__init__(self)
        
        container = tk.Frame(self)
        container.grid_columnconfigure(0, weight=1)
        container.grid_rowconfigure(0, weight=1)
        container.pack(side="top", fill="both", expand=True)

        self.frameslist = {}

        for frame in (Create,):
            frame_occurrence = frame.__name__
            active_frame = frame(parent=container, controller=self)
            self.frameslist[frame_occurrence] = active_frame
            active_frame.grid(row=0, column=0, sticky="snew")

        self.show_frame("Create")
            
    def show_frame(self, frame_occurrence):
        active_frame = self.frameslist[frame_occurrence]
        active_frame.tkraise()

class Create(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller

        #For all widgets (nested list, 2 widgets per row)
        self.inputlist = []
        #For just the entries
        self.newinputlist = []

        #Create two labels, add them into the inputlist to be iterated
        labels = [tk.Label(self, text="Field"), tk.Label(self, text="Values")]
        self.inputlist.append(labels[:])

        #Insert the labels from the list
        for toplabels in range(1):
            self.inputlist[toplabels][0].grid(row=toplabels, column=0, padx=10, pady=5)
            self.inputlist[toplabels][1].grid(row=toplabels, column=1, padx=10, pady=5)

        #Create the first two entry boxes, append them to the inputlist, and newinput list
        first_entries = [tk.Entry(self, borderwidth=5), tk.Text(self, borderwidth=5, height= 5, width=20)]
        self.newinputlist.append(first_entries[:])
        self.inputlist.append(first_entries[:])

        #Insert the entries from the newinputlist
        for x in range(0, len(self.newinputlist) + 1):
            self.newinputlist[0][x].grid(row=1, column=x, padx=10, pady=5)

        #Create two buttons (Both share same row), append them to list
        button_input_1 = [tk.Button(self, text="ADD FIELD/VALUE", command=self.add_insert), tk.Button(self, text="BACK")]
        self.inputlist.append(button_input_1[:])
        
        #Insert buttons at the bottom of the grid
        for button in range(len(self.inputlist) - 2, len(self.inputlist)):
            self.inputlist[button][0].grid(row=button, column=0, padx=10, pady=5)
            self.inputlist[button][1].grid(row=button, column=1, padx=10, pady=5)

    def add_insert(self):
        #Create two new entries, append them to the list 
        add_input = [tk.Entry(self, borderwidth=5), tk.Text(self, borderwidth=5, height= 5, width=20)]
        self.inputlist.insert(-1, add_input)
        self.newinputlist.append(add_input)
        
        #Because there are new entry boxes, old grid should be forgotten
        for widget in self.children.values():
            widget.grid_forget()

        #Use the index for the row, get all widgets and place them again
        for index, widgets in enumerate(self.inputlist):
            widget_one = widgets[0]
            widget_two = widgets[1]

            widget_one.grid(row=index, column=0, padx=10, pady=5)
            widget_two.grid(row=index, column=1, padx=10)

        #Create scrollbar when this button is pressed
        scrollbar = tk.Scrollbar(self, orient="vertical")
        scrollbar.grid(row=0, column=2, stick="ns", rowspan=len(self.inputlist) + 1)

if __name__ == "__main__":
    app = Insert()
    app.maxsize(0, 500)
    app.mainloop()
4

1 回答 1

0

您可以创建 aCanvas并将您的Entry对象插入到Frame.

这是一个简化的示例,它Buttons使用canvas.create_window.

import tkinter as tk
 
root = tk.Tk()      
# essential to enable full window resizing
root.rowconfigure(0, weight = 1)
root.columnconfigure(0, weight = 1)

# scrollregion is also essential when using scrollbars
canvas = tk.Canvas(
    root, scrollregion = "0 0 2000 1000", width = 400, height = 400)
canvas.grid(row = 0, column = 0, sticky = tk.NSEW)

scroll = tk.Scrollbar(root, orient = tk.VERTICAL, command = canvas.yview)
scroll.grid(row = 0, column = 1, sticky = tk.NS)
canvas.config(yscrollcommand = scroll.set)

# I've used a labelframe instead of frame so button are neatly collected and named
frame = tk.LabelFrame(root, labelanchor = tk.N, text = "Buttonpad")

# Note I've placed buttons in frame
for fila in range(20):
    for col in range(5):
        btn = tk.Button(frame, text = f"{fila}-{col}")
        btn.grid(row = fila, column = col, sticky = tk.NSEW)

# Frame is now inserted into canvas via create_window method
item = canvas.create_window(( 2, 2 ), anchor = tk.NW,  window = frame )

root.mainloop()
于 2021-10-13T23:48:09.017 回答