0

I am trying to create a scroll bar for a window created in my gui using tk.Toplevel. The frame currently often fills up with long grids of strings that are added one buy one. The rest of the GUI allows a user to write to a series of fields which are displayed one after another using a grid.

The data might look something like this:

sadas sfafaf fsaafads fdsafsf dfsfdsf fssfs fsfsfsfs fsfsfs sfsfsfs fsssfsfsf

sadas sfafaf fsaafads fdsafsf dfsfdsf fssfs fsfsfsfs fsfsfs sfsfsfs fsssfsfsf

sadas sfafaf fsaafads fdsafsf dfsfdsf fssfs fsfsfsfs fsfsfs sfsfsfs fsssfsfsf

I used this post: Adding a scrollbar to a group of widgets in Tkinter as a guide to get to where I am right now. Currently, when I open the window the scroll bar appears on the left side. As soon as I add a line the GUI freezes and I have to close it.

Here is the relevant code:

class data_popup():
    def __init__(self, parent):
        self.parent = parent
        self.top = tk.Toplevel(parent)
        self.top.title("Current Data")
        self.top.protocol('WM_DELETE_WINDOW', self.stop_close)
        #Frames
        self.header_labels_frame = tk.Frame(self.top)
        self.header_labels_frame.pack(side = tk.TOP)
        self.data_frame_1 = tk.Frame(self.top)
        self.data_frame_1.pack(side = tk.BOTTOM)        
        self.header_labels_list = []


        self.data_canvas = tk.Canvas(self.data_frame_1, borderwidth = 0)
        self.data_frame = tk.Frame(self.data_canvas)
        self.scrollbar = tk.Scrollbar(self.data_frame, orient = 'vertical', command = self.data_canvas.yview)
        self.data_canvas.configure(yscrollcommand = self.scrollbar.set)
        self.scrollbar.pack(side = 'right', fill = 'y')
        self.data_canvas.pack(side = "bottom", fill = 'both', expand = True)
        self.data_canvas.create_window((4,4), window = self.data_frame, anchor = 'nw', tags = "self.frame")
        self.data_frame.bind("<Configure>", self.myfunction)
        self.create_header()
        self.data_row_list = list()

    def myfunction(self, event):
        self.data_canvas.configure(scrollregion=self.data_canvas.bbox("all"))
Community
  • 1
  • 1
DustySack
  • 120
  • 2
  • 9
  • 1
    The indentation of your code is incorrect. – Bryan Oakley Jun 19 '15 at 18:23
  • 2
    _"As soon as I add a line the GUI freezes and I have to close it. "_ You're not using both `grid` and `pack` in the same window and/or frame, are you? That makes Tkinter enter an infinite loop. – Kevin Jun 19 '15 at 18:23
  • The indentation is normal on my editor it just copy pasted wrong. I will fix it. – DustySack Jun 19 '15 at 18:24

2 Answers2

2

Often, when a GUI freezes in the manner you describe, it is because you use both grid and pack in the same frame. You aren't showing the code where you add something to the frame, but my guess is, that is what is happening.

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
  • 1
    You are correct! I am the labels using a grid. How do I add the canvas to the frame with grid instead of pack? (As in what should the row/column values be) – DustySack Jun 19 '15 at 18:27
  • The scrollbar and canvas are currently using pack. So I need grid for the scrollbar as well. – DustySack Jun 19 '15 at 18:28
  • Making them both grid (used row=0,column=0) for both prevents crashes, but it alters the display. Instead of lines every time I try to write it just displays one dot per line. The scroll bar also disappears when I write a line and reappears if I delete them all. – DustySack Jun 19 '15 at 18:38
  • @DustySack: my recommendation is to forget about the scrollbar initially, and focus on getting the frame to look right. Once you get that, _then_ you can put the frame in a canvas and add scrollbars. Don't try to do too much at one time. – Bryan Oakley Jun 19 '15 at 18:48
  • That is what I did. Adding the scrollbar is what broke how it looks (after I added grid instead of pack, before nothing would display because it froze) – DustySack Jun 19 '15 at 18:51
0

For people who find this post on google I was able to use the information from this post to complete what I needed Adding a scrollbar to a frame using Tkinter (Python)

Community
  • 1
  • 1
DustySack
  • 120
  • 2
  • 9