skizziks_53

canvas issues #1

Dec 17th, 2019
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.20 KB | None | 0 0
  1. '''
  2. December 17, 2019
  3.  
  4. This is an example application that allows scrolling a canvas and dragging boxes on a canvas.
  5. I can't figure out how to make use of it though--I can't find any events that I can bind to to get information
  6. about any changes that occur.
  7.  
  8. The canvas visible area is 300 x 300 px, but the scrollable area is 600 x 600 px.
  9.  
  10. 1. How can I update the canvas horizontal & vertical scroll labels when that occurs?
  11.  
  12. 2. How can I tell which box was moved after it was moved, or get its actual x-y location on the full scrollable canvas range?
  13. '''
  14.  
  15. import tkinter as tk
  16. from tkinter import ttk
  17. from functools import partial
  18.  
  19.  
  20. root = tk.Tk()
  21. root.title( "Scroll/Drag Canvas Example 1.0" )
  22. window_width = 900
  23. window_height = 700
  24. root.minsize(window_width, window_height)
  25. window_position_x = 20
  26. window_position_y = 20
  27. root.geometry('%dx%d+%d+%d' % (window_width, window_height, window_position_x, window_position_y))
  28. root.resizable(True, True)
  29. window_test_color = '#%02x%02x%02x' % (160, 160, 160)
  30. root.config(bg=window_test_color)
  31.  
  32. label_test_color = '#%02x%02x%02x' % (255, 180, 180)
  33.  
  34. # @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  35. # Below is the scrolling functionality (working)
  36.  
  37. canvas_test_color = '#%02x%02x%02x' % (100, 160, 100)
  38. canvas1 = tk.Canvas(master=root, width=300, height=300, scrollregion=(0,0,600,600), bg=canvas_test_color)
  39. canvas1.place(x=30, y=30)
  40.  
  41. hbar1 = tk.Scrollbar(master=root,orient=tk.HORIZONTAL)
  42. hbar1.place(x=30, y=360, width=200, height=30)
  43.  
  44. vbar1 = tk.Scrollbar(master=root,orient=tk.VERTICAL)
  45. vbar1.place(x=360, y=30, width=30, height=200)
  46.  
  47. hbar1.config(command=canvas1.xview)
  48. vbar1.config(command=canvas1.yview)
  49. canvas1.config(xscrollcommand=hbar1.set, yscrollcommand=vbar1.set)
  50.  
  51. # @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  52.  
  53. # Below is trying to get the canvas scroll values.
  54. # Nothing I tried worked, at all.
  55.  
  56. def show_canvas_scroll_values(event):
  57.     canvas = event.widget
  58.     x = canvas.canvasx(event.x)
  59.     y = canvas.canvasy(event.y)
  60.     #print canvas.find_closest(x, y)
  61.     canvas_hposition_label.config(text=x)
  62.     canvas_vposition_label.config(text=y)
  63.  
  64. #canvas1.bind('<Configure>', show_canvas_scroll_values)
  65.  
  66.  
  67. # @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  68. # This section is just four labels to read out the canvas scroll values
  69. # and the last-dragged-box actual xy values, on the 600x600 canvas area.
  70.  
  71. xpos=430
  72. ypos=50
  73. vspacer425 = 30
  74. someLabel = tk.Label(master=root, text="canvas vertical scroll:")
  75. someLabel.place(x=xpos, y=ypos)
  76. xpos=570
  77. canvas_hposition_label = tk.Label(master=root, text="???", bg=label_test_color)
  78. canvas_hposition_label.place(x=xpos, y=ypos)
  79.  
  80. xpos=430
  81. ypos=ypos + vspacer425
  82. someLabel = tk.Label(master=root, text="canvas horizontal scroll:")
  83. someLabel.place(x=xpos, y=ypos)
  84. xpos=570
  85. canvas_vposition_label = tk.Label(master=root, text="???", bg=label_test_color)
  86. canvas_vposition_label.place(x=xpos, y=ypos)
  87.  
  88. xpos=430
  89. ypos=ypos + vspacer425 + vspacer425
  90. someLabel = tk.Label(master=root, text="box xpos:")
  91. someLabel.place(x=xpos, y=ypos)
  92. xpos=530
  93. box_xposition_label = tk.Label(master=root, text="???", bg=label_test_color)
  94. box_xposition_label.place(x=xpos, y=ypos)
  95.  
  96. xpos=430
  97. ypos=ypos + vspacer425
  98. someLabel = tk.Label(master=root, text="box ypos:")
  99. someLabel.place(x=xpos, y=ypos)
  100. xpos=530
  101. box_yposition_label = tk.Label(master=root, text="???", bg=label_test_color)
  102. box_yposition_label.place(x=xpos, y=ypos)
  103.  
  104.  
  105.  
  106. # @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  107. # this just draws the grid on the canvas.
  108.     # canvas1 scroll region is 600 x 600
  109.  
  110. def draw_test_lines_on_canvas():
  111.     tcolor1 = '#%02x%02x%02x' % (0, 0, 0)
  112.     vspacing = 30
  113.     hspacing = 100
  114.     loopcount = 0
  115.     tValue = vspacing
  116.     while loopcount < 20:
  117.         canvas1.create_line(0, tValue, 600, tValue, fill=tcolor1)
  118.         tValue += vspacing
  119.         loopcount += 1
  120.     loopcount = 0
  121.     tValue = hspacing
  122.     while loopcount < 7:
  123.         canvas1.create_line(tValue, 0, tValue, 600, fill=tcolor1)
  124.         tValue += hspacing
  125.         loopcount += 1
  126.  
  127. draw_test_lines_on_canvas()
  128.  
  129. # @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  130. # Below is the code for putting the 3 boxes on the canvas, and dragging them.
  131.  
  132. def canvas1_remember(event):
  133.     global canvas1_former_pos
  134.     canvas1_former_pos = event.x, event.y
  135.  
  136. canvas1.bind('<Button>', canvas1_remember)
  137.  
  138. def ondrag(box, event):
  139.     global canvas1_former_pos
  140.     canvas1.tag_raise(box) # <------------------------------------------ ???????????? what is this tag that is raised ???
  141.     canvas1.move(box, event.x-canvas1_former_pos[0], event.y-canvas1_former_pos[1])
  142.     canvas1_remember(event)
  143.  
  144. def add_box(x_pos, y_pos, color):
  145.     box = canvas1.create_rectangle(x_pos, y_pos, x_pos+dragBox_width, y_pos+dragBox_height, outline="black", fill=color)
  146.     canvas1.tag_bind(box, '<B1-Motion>', partial(ondrag, box))
  147.  
  148. canvas1_former_pos = 0,0
  149. dragBox_width = 80
  150. dragBox_height = 20
  151.  
  152. add_box(80, 100, 'red')
  153. add_box(80, 200, 'green')
  154. add_box(200, 200, 'blue')
  155.  
  156.  
  157. # @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  158.  
  159.  
  160. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment