Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- '''
- December 17, 2019
- This is an example application that allows scrolling a canvas and dragging boxes on a canvas.
- 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
- about any changes that occur.
- The canvas visible area is 300 x 300 px, but the scrollable area is 600 x 600 px.
- 1. How can I update the canvas horizontal & vertical scroll labels when that occurs?
- 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?
- '''
- import tkinter as tk
- from tkinter import ttk
- from functools import partial
- root = tk.Tk()
- root.title( "Scroll/Drag Canvas Example 1.0" )
- window_width = 900
- window_height = 700
- root.minsize(window_width, window_height)
- window_position_x = 20
- window_position_y = 20
- root.geometry('%dx%d+%d+%d' % (window_width, window_height, window_position_x, window_position_y))
- root.resizable(True, True)
- window_test_color = '#%02x%02x%02x' % (160, 160, 160)
- root.config(bg=window_test_color)
- label_test_color = '#%02x%02x%02x' % (255, 180, 180)
- # @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
- # Below is the scrolling functionality (working)
- canvas_test_color = '#%02x%02x%02x' % (100, 160, 100)
- canvas1 = tk.Canvas(master=root, width=300, height=300, scrollregion=(0,0,600,600), bg=canvas_test_color)
- canvas1.place(x=30, y=30)
- hbar1 = tk.Scrollbar(master=root,orient=tk.HORIZONTAL)
- hbar1.place(x=30, y=360, width=200, height=30)
- vbar1 = tk.Scrollbar(master=root,orient=tk.VERTICAL)
- vbar1.place(x=360, y=30, width=30, height=200)
- hbar1.config(command=canvas1.xview)
- vbar1.config(command=canvas1.yview)
- canvas1.config(xscrollcommand=hbar1.set, yscrollcommand=vbar1.set)
- # @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
- # Below is trying to get the canvas scroll values.
- # Nothing I tried worked, at all.
- def show_canvas_scroll_values(event):
- canvas = event.widget
- x = canvas.canvasx(event.x)
- y = canvas.canvasy(event.y)
- #print canvas.find_closest(x, y)
- canvas_hposition_label.config(text=x)
- canvas_vposition_label.config(text=y)
- #canvas1.bind('<Configure>', show_canvas_scroll_values)
- # @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
- # This section is just four labels to read out the canvas scroll values
- # and the last-dragged-box actual xy values, on the 600x600 canvas area.
- xpos=430
- ypos=50
- vspacer425 = 30
- someLabel = tk.Label(master=root, text="canvas vertical scroll:")
- someLabel.place(x=xpos, y=ypos)
- xpos=570
- canvas_hposition_label = tk.Label(master=root, text="???", bg=label_test_color)
- canvas_hposition_label.place(x=xpos, y=ypos)
- xpos=430
- ypos=ypos + vspacer425
- someLabel = tk.Label(master=root, text="canvas horizontal scroll:")
- someLabel.place(x=xpos, y=ypos)
- xpos=570
- canvas_vposition_label = tk.Label(master=root, text="???", bg=label_test_color)
- canvas_vposition_label.place(x=xpos, y=ypos)
- xpos=430
- ypos=ypos + vspacer425 + vspacer425
- someLabel = tk.Label(master=root, text="box xpos:")
- someLabel.place(x=xpos, y=ypos)
- xpos=530
- box_xposition_label = tk.Label(master=root, text="???", bg=label_test_color)
- box_xposition_label.place(x=xpos, y=ypos)
- xpos=430
- ypos=ypos + vspacer425
- someLabel = tk.Label(master=root, text="box ypos:")
- someLabel.place(x=xpos, y=ypos)
- xpos=530
- box_yposition_label = tk.Label(master=root, text="???", bg=label_test_color)
- box_yposition_label.place(x=xpos, y=ypos)
- # @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
- # this just draws the grid on the canvas.
- # canvas1 scroll region is 600 x 600
- def draw_test_lines_on_canvas():
- tcolor1 = '#%02x%02x%02x' % (0, 0, 0)
- vspacing = 30
- hspacing = 100
- loopcount = 0
- tValue = vspacing
- while loopcount < 20:
- canvas1.create_line(0, tValue, 600, tValue, fill=tcolor1)
- tValue += vspacing
- loopcount += 1
- loopcount = 0
- tValue = hspacing
- while loopcount < 7:
- canvas1.create_line(tValue, 0, tValue, 600, fill=tcolor1)
- tValue += hspacing
- loopcount += 1
- draw_test_lines_on_canvas()
- # @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
- # Below is the code for putting the 3 boxes on the canvas, and dragging them.
- def canvas1_remember(event):
- global canvas1_former_pos
- canvas1_former_pos = event.x, event.y
- canvas1.bind('<Button>', canvas1_remember)
- def ondrag(box, event):
- global canvas1_former_pos
- canvas1.tag_raise(box) # <------------------------------------------ ???????????? what is this tag that is raised ???
- canvas1.move(box, event.x-canvas1_former_pos[0], event.y-canvas1_former_pos[1])
- canvas1_remember(event)
- def add_box(x_pos, y_pos, color):
- box = canvas1.create_rectangle(x_pos, y_pos, x_pos+dragBox_width, y_pos+dragBox_height, outline="black", fill=color)
- canvas1.tag_bind(box, '<B1-Motion>', partial(ondrag, box))
- canvas1_former_pos = 0,0
- dragBox_width = 80
- dragBox_height = 20
- add_box(80, 100, 'red')
- add_box(80, 200, 'green')
- add_box(200, 200, 'blue')
- # @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
- root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment