Guest User

Untitled

a guest
Jun 25th, 2018
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.77 KB | None | 0 0
  1. class Scrollable(tk.Frame):
  2. """
  3. Make a frame scrollable with scrollbar on the right.
  4. After adding or removing widgets to the scrollable frame,
  5. call the update() method to refresh the scrollable area.
  6. """
  7.  
  8. def __init__(self, frame, **kwargs):
  9. scrollbar = tk.Scrollbar(win,)
  10. scrollbar.grid(row=0, column=1, sticky=tk.NS)#pack(side=tk.LEFT, fill=tk.Y, expand=True)
  11.  
  12. w = kwargs.get('width', MAP_WIDTH)
  13. h = kwargs.get('height', MAP_HEIGHT)
  14.  
  15.  
  16. self.canvas = tk.Canvas(frame, yscrollcommand=scrollbar.set, height=h-100, width=w-100, # produce
  17. highlightthickness=0, borderwidth=5, background='#123456')
  18. self.canvas.grid(row=2)
  19.  
  20. scrollbar.config(command=self.canvas.yview)
  21.  
  22. self.canvas.bind('<Configure>', self._fill_canvas)
  23.  
  24. super().__init__(frame, **kwargs)
  25. self.windows_item = self.canvas.create_window(0,0, window=self, anchor=tk.NW,)
  26.  
  27. def _fill_canvas(self, event):
  28. "Enlarge the windows item to the canvas width"
  29. canvas_width = event.width
  30. self.canvas.itemconfig(self.windows_item, width = canvas_width)
  31.  
  32. def update(self):
  33. "Update the canvas and the scrollregion"
  34. self.update_idletasks()
  35. self.canvas.config(scrollregion=self.canvas.bbox(tk.ALL))
  36.  
  37. win = tk.Tk()
  38. MAP_WIDTH = 25*columns
  39. MAP_HEIGHT = 25*rows
  40. whole_frame = tk.Frame(win, relief=tk.RIDGE, borderwidth=10, )
  41. whole_frame.grid(row=0, column=0)
  42. top_frame = tk.Frame(whole_frame, width=MAP_WIDTH, padx=10)
  43. top_frame.grid(sticky=tk.EW)
  44. divider = tk.Frame(whole_frame, relief=tk.RAISED,
  45. height=10, width=MAP_WIDTH, borderwidth=10)
  46. divider.grid(row=1)
  47. bottom_frame = Scrollable(whole_frame,)
  48.  
  49.  
  50. realmap = [tk.Button(...) for i in range(rows*columns)]
  51. for i in realmap:
  52. i.grid(...)
  53.  
  54. bottom_frame.update()
  55. win.update()
Add Comment
Please, Sign In to add comment