如何在窗口中保留Tkinter画布?

时间:2019-08-16 15:48:34

标签: python tkinter tkinter-canvas

运行代码时,我的输入字段网格不适合窗口,这意味着我必须扩展窗口才能访问下方的输入字段和按钮。我希望能够通过滚动来找到他们。

我尝试了各种框架和画布的组合,包括将输入字段直接放在画布上,但是我从来没有能够创建一个与窗口大小相同的画布(因此比包含的条目网格小)在其中)。

def __init__(self, window):
        # parameter entry fields below

        column_headers = ["Duration (ns)", "SPDT state", "SP4T state", "RF relative wave"]
        row_number=50
        self.entries = []
        canvas = tk.Canvas(window, width=700, height=600)
        frame=tk.Frame(canvas)
        frame.grid(row=0, column=0)
        canvas.configure(scrollregion=frame.bbox("all"))

        for col_num, col_name in enumerate(column_headers):
            tk.Label(frame, text = col_name).grid(row = 0, column = col_num)
        for row_num in range(row_number):              # Creates grid of entry fields and stores locations in a list of lists. 
            self.entries.append([])                    # Entry field contents accessed by self.entries[row_num][col_num].get() (both starting at 0)
            for col_num, col_name in enumerate(column_headers):
                self.entries[row_num].append(tk.StringVar())
                self.entries[row_num][col_num] = tk.Entry(frame)
                self.entries[row_num][col_num].grid(row = row_num+1, column = col_num)

        tk.Button(frame, text = "Update Parameters", command=self.get_params).grid(row = row_number+4)
        tk.Button(frame, text = "Run Sweep", command= run_expt).grid(row = row_number+4, column = 1)
        tk.Button(frame, text = "Abort", command = abort).grid(row = row_number+4, column = 2)

        # data storage stuff below

        tk.Label(frame, text="File Name").grid(sticky='W', row=row_number+3, column=0)
        self.fileNameEntry = tk.StringVar()
        self.fileNameEntry = tk.Entry(frame)
        self.fileNameEntry.grid(row=row_number+3, column=1)

        vbar = tk.Scrollbar(window, orient=tk.VERTICAL, command=canvas.yview)
        canvas.configure(yscrollcommand=vbar.set)
        vbar.pack(side=tk.RIGHT, fill=tk.Y)
        canvas.pack(fill=tk.BOTH)


window=tk.Tk()
window.geometry("700x600")
EPR=EPRGUI(window)
window.mainloop()

所以没有人建议这样做,我想指出的是,我的代码中确实具有所有按钮的功能,但是从这个问题中省略了它们,以使其读起来更快。

0 个答案:

没有答案