0

When I run my code, my grid of entry fields does not fit within the window, meaning I have to expand the window in order to access the lower entry fields and the buttons. I want to be able to get to them by scrolling instead.

I have tried various combinations of frames and canvasses, including putting the entry fields directly on the canvas, but at no point have I been able to create a canvas the size of the window(and therefore smaller than the grid of entries contained within it).

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

Just so no one suggests this, I'd like to point out that I do have functions for all the buttons in my code, but have omitted them from this question to make it a bit quicker to read.

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
Patrick
  • 1
  • 1
  • Read [Adding a scrollbar to a group of widgets in Tkinter](https://stackoverflow.com/a/3092341/7414759) – stovfl Aug 16 '19 at 16:04
  • Thanks, I'll take another look at that, but I have already seen that page and have been unable to work out why that works and mine doesn't – Patrick Aug 17 '19 at 17:55
  • The main diff are, you don't use `self.frame.bind(""...` – stovfl Aug 17 '19 at 18:00
  • And that binds the frame to the canvas so that they scroll together, does it? I don't understand the what the is pointing to (though I was able to use it to fix my tkinter GUI). – Patrick Aug 21 '19 at 15:16
  • The `` event gets fired your GUI is shown or gets resized. At initalisation your `frame.bbox` are of size *1x1* pixel. So your `.configure(scrollregion=frame.bbox("all")` is **to early**. Therefore you need to do `.configure(scrollregion=frame.bbox("all")` within the `callback` of the `` event. – stovfl Aug 21 '19 at 18:09

0 Answers0