Advertisement
Guest User

Untitled

a guest
Jul 25th, 2017
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.57 KB | None | 0 0
  1. from Tkinter import *
  2.  
  3. def data():
  4. for i in range(50):
  5. Label(frame,text=i).grid(row=i,column=0)
  6. Label(frame,text="my text"+str(i)).grid(row=i,column=1)
  7. Label(frame,text="..........").grid(row=i,column=2)
  8.  
  9. def myfunction(event):
  10. canvas.configure(scrollregion=canvas.bbox("all"),width=200,height=200)
  11.  
  12. root=Tk()
  13. sizex = 800
  14. sizey = 600
  15. posx = 100
  16. posy = 100
  17. root.wm_geometry("%dx%d+%d+%d" % (sizex, sizey, posx, posy))
  18.  
  19. myframe=Frame(root,relief=GROOVE,width=50,height=100,bd=1)
  20. myframe.place(x=10,y=10)
  21.  
  22. canvas=Canvas(myframe)
  23. frame=Frame(canvas)
  24. myscrollbar=Scrollbar(myframe,orient="vertical",command=canvas.yview)
  25. canvas.configure(yscrollcommand=myscrollbar.set)
  26.  
  27. myscrollbar.pack(side="right",fill="y")
  28. canvas.pack(side="left")
  29. canvas.create_window((0,0),window=frame,anchor='nw')
  30. frame.bind("<Configure>",myfunction)
  31. data()
  32. root.mainloop()
  33.  
  34. from Tkinter import * # from x import * is bad practice
  35. from ttk import *
  36.  
  37. # http://tkinter.unpythonic.net/wiki/VerticalScrolledFrame
  38.  
  39. class VerticalScrolledFrame(Frame):
  40. """A pure Tkinter scrollable frame that actually works!
  41. * Use the 'interior' attribute to place widgets inside the scrollable frame
  42. * Construct and pack/place/grid normally
  43. * This frame only allows vertical scrolling
  44.  
  45. """
  46. def __init__(self, parent, *args, **kw):
  47. Frame.__init__(self, parent, *args, **kw)
  48.  
  49. # create a canvas object and a vertical scrollbar for scrolling it
  50. vscrollbar = Scrollbar(self, orient=VERTICAL)
  51. vscrollbar.pack(fill=Y, side=RIGHT, expand=FALSE)
  52. canvas = Canvas(self, bd=0, highlightthickness=0,
  53. yscrollcommand=vscrollbar.set)
  54. canvas.pack(side=LEFT, fill=BOTH, expand=TRUE)
  55. vscrollbar.config(command=canvas.yview)
  56.  
  57. # reset the view
  58. canvas.xview_moveto(0)
  59. canvas.yview_moveto(0)
  60.  
  61. # create a frame inside the canvas which will be scrolled with it
  62. self.interior = interior = Frame(canvas)
  63. interior_id = canvas.create_window(0, 0, window=interior,
  64. anchor=NW)
  65.  
  66. # track changes to the canvas and frame width and sync them,
  67. # also updating the scrollbar
  68. def _configure_interior(event):
  69. # update the scrollbars to match the size of the inner frame
  70. size = (interior.winfo_reqwidth(), interior.winfo_reqheight())
  71. canvas.config(scrollregion="0 0 %s %s" % size)
  72. if interior.winfo_reqwidth() != canvas.winfo_width():
  73. # update the canvas's width to fit the inner frame
  74. canvas.config(width=interior.winfo_reqwidth())
  75. interior.bind('<Configure>', _configure_interior)
  76.  
  77. def _configure_canvas(event):
  78. if interior.winfo_reqwidth() != canvas.winfo_width():
  79. # update the inner frame's width to fill the canvas
  80. canvas.itemconfigure(interior_id, width=canvas.winfo_width())
  81. canvas.bind('<Configure>', _configure_canvas)
  82.  
  83.  
  84. if __name__ == "__main__":
  85.  
  86. class SampleApp(Tk):
  87. def __init__(self, *args, **kwargs):
  88. root = Tk.__init__(self, *args, **kwargs)
  89.  
  90.  
  91. self.frame = VerticalScrolledFrame(root)
  92. self.frame.pack()
  93. self.label = Label(text="Shrink the window to activate the scrollbar.")
  94. self.label.pack()
  95. buttons = []
  96. for i in range(10):
  97. buttons.append(Button(self.frame.interior, text="Button " + str(i)))
  98. buttons[-1].pack()
  99.  
  100. app = SampleApp()
  101. app.mainloop()
  102.  
  103. class ScrolledWindow(tk.Frame):
  104. """
  105. 1. Master widget gets scrollbars and a canvas. Scrollbars are connected
  106. to canvas scrollregion.
  107.  
  108. 2. self.scrollwindow is created and inserted into canvas
  109.  
  110. Usage Guideline:
  111. Assign any widgets as children of <ScrolledWindow instance>.scrollwindow
  112. to get them inserted into canvas
  113.  
  114. __init__(self, parent, canv_w = 400, canv_h = 400, *args, **kwargs)
  115. docstring:
  116. Parent = master of scrolled window
  117. canv_w - width of canvas
  118. canv_h - height of canvas
  119.  
  120. """
  121.  
  122.  
  123. def __init__(self, parent, canv_w = 400, canv_h = 400, *args, **kwargs):
  124. """Parent = master of scrolled window
  125. canv_w - width of canvas
  126. canv_h - height of canvas
  127.  
  128. """
  129. super().__init__(parent, *args, **kwargs)
  130.  
  131. self.parent = parent
  132.  
  133. # creating a scrollbars
  134. self.xscrlbr = ttk.Scrollbar(self.parent, orient = 'horizontal')
  135. self.xscrlbr.grid(column = 0, row = 1, sticky = 'ew', columnspan = 2)
  136. self.yscrlbr = ttk.Scrollbar(self.parent)
  137. self.yscrlbr.grid(column = 1, row = 0, sticky = 'ns')
  138. # creating a canvas
  139. self.canv = tk.Canvas(self.parent)
  140. self.canv.config(relief = 'flat',
  141. width = 10,
  142. heigh = 10, bd = 2)
  143. # placing a canvas into frame
  144. self.canv.grid(column = 0, row = 0, sticky = 'nsew')
  145. # accociating scrollbar comands to canvas scroling
  146. self.xscrlbr.config(command = self.canv.xview)
  147. self.yscrlbr.config(command = self.canv.yview)
  148.  
  149. # creating a frame to inserto to canvas
  150. self.scrollwindow = ttk.Frame(self.parent)
  151.  
  152. self.canv.create_window(0, 0, window = self.scrollwindow, anchor = 'nw')
  153.  
  154. self.canv.config(xscrollcommand = self.xscrlbr.set,
  155. yscrollcommand = self.yscrlbr.set,
  156. scrollregion = (0, 0, 100, 100))
  157.  
  158. self.yscrlbr.lift(self.scrollwindow)
  159. self.xscrlbr.lift(self.scrollwindow)
  160. self.scrollwindow.bind('<Configure>', self._configure_window)
  161. self.scrollwindow.bind('<Enter>', self._bound_to_mousewheel)
  162. self.scrollwindow.bind('<Leave>', self._unbound_to_mousewheel)
  163.  
  164. return
  165.  
  166. def _bound_to_mousewheel(self, event):
  167. self.canv.bind_all("<MouseWheel>", self._on_mousewheel)
  168.  
  169. def _unbound_to_mousewheel(self, event):
  170. self.canv.unbind_all("<MouseWheel>")
  171.  
  172. def _on_mousewheel(self, event):
  173. self.canv.yview_scroll(int(-1*(event.delta/120)), "units")
  174.  
  175. def _configure_window(self, event):
  176. # update the scrollbars to match the size of the inner frame
  177. size = (self.scrollwindow.winfo_reqwidth(), self.scrollwindow.winfo_reqheight())
  178. self.canv.config(scrollregion='0 0 %s %s' % size)
  179. if self.scrollwindow.winfo_reqwidth() != self.canv.winfo_width():
  180. # update the canvas's width to fit the inner frame
  181. self.canv.config(width = self.scrollwindow.winfo_reqwidth())
  182. if self.scrollwindow.winfo_reqheight() != self.canv.winfo_height():
  183. # update the canvas's width to fit the inner frame
  184. self.canv.config(height = self.scrollwindow.winfo_reqheight())
  185.  
  186. f = Tkinter.Frame(self.master,width=3)
  187. f.grid(row=2, column=0, columnspan=8, rowspan=10, pady=30, padx=30)
  188. f.config(width=5)
  189. self.tree = ttk.Treeview(f, selectmode="extended")
  190. scbHDirSel =tk.Scrollbar(f, orient=Tkinter.HORIZONTAL, command=self.tree.xview)
  191. scbVDirSel =tk.Scrollbar(f, orient=Tkinter.VERTICAL, command=self.tree.yview)
  192. self.tree.configure(yscrollcommand=scbVDirSel.set, xscrollcommand=scbHDirSel.set)
  193. self.tree["columns"] = (self.columnListOutput)
  194. self.tree.column("#0", width=40)
  195. self.tree.heading("#0", text='SrNo', anchor='w')
  196. self.tree.grid(row=2, column=0, sticky=Tkinter.NSEW,in_=f, columnspan=10, rowspan=10)
  197. scbVDirSel.grid(row=2, column=10, rowspan=10, sticky=Tkinter.NS, in_=f)
  198. scbHDirSel.grid(row=14, column=0, rowspan=2, sticky=Tkinter.EW,in_=f)
  199. f.rowconfigure(0, weight=1)
  200. f.columnconfigure(0, weight=1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement