Advertisement
skizziks_53

Canvas tag bug? v1.0

Feb 29th, 2020
413
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.67 KB | None | 0 0
  1. import tkinter as tk
  2. from tkinter import ttk
  3. from tkinter import messagebox
  4. from functools import partial
  5.  
  6.  
  7. '''
  8. Ctag Tester v1.0
  9. 29 February 2020
  10. Written for Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 27 2018, 04:06:47) [MSC v.1914 32 bit (Intel)] on win32 / Windows 10 / VS Code (ide)
  11.  
  12. This is a demo of a tkinter / canvas bug. Or something. (?)
  13.  
  14. 1. Each of the two squares on the canvas should report its tag for a mouse-1-down and mouse-1-up event.
  15.  
  16. 2. It always reports properly if you click and release on the same item.
  17.  
  18. 3. PROBLEM: If you click down on one square and then drag to the other square, the mouse-up event gives the same tag as the mouse-down event, even though they did not occur on the same item.
  19.  
  20. 4. The mouse x-y coordinates are also reported, and those always report correctly wherever those two events occur.
  21.  
  22. 5. What I assumed should happen is that the mouse-down event should get the tag of the item that it occurs on, and the mouse-up event should get the tag of the item that it occurs on.
  23.  
  24. 6. ???
  25.  
  26. 7. Profit!
  27. '''
  28.  
  29.  
  30. def main():
  31.     program = TestProgram1()
  32.     program.window.mainloop()
  33.  
  34. class TestProgram1:
  35.  
  36.     def __init__(self):
  37.         self.window = tk.Tk()
  38.         self.window.title( "Ctag Tester v1.0" )
  39.         self.window_width = 800
  40.         self.window_height = 600
  41.         self.window.minsize(self.window_width, self.window_height)
  42.         self.window_position_x = 40
  43.         self.window_position_y = 40
  44.         self.window.geometry('%dx%d+%d+%d' % (self.window_width, self.window_height, self.window_position_x, self.window_position_y))
  45.         self.window.resizable(False, False)
  46.  
  47.  
  48.  
  49.         x_pos = 20
  50.         y_pos = 20
  51.         self.staticlabel_38 = tk.Label(master=self.window, text="mousedown xy:")
  52.         self.staticlabel_38.place(x=x_pos, y=y_pos)
  53.         x_pos = 180
  54.         y_pos = 20
  55.         self.staticlabel_42 = tk.Label(master=self.window, text="mouseup xy:")
  56.         self.staticlabel_42.place(x=x_pos, y=y_pos)
  57.  
  58.         x_pos = 20
  59.         y_pos = 50
  60.         self.label_mousedown = tk.Label(master=self.window, text="[none]")
  61.         self.label_mousedown.place(x=x_pos, y=y_pos)
  62.         x_pos = 180
  63.         y_pos = 50
  64.         self.label_mouseup = tk.Label(master=self.window, text="[none]")
  65.         self.label_mouseup.place(x=x_pos, y=y_pos)
  66.  
  67.         x_pos = 20
  68.         y_pos = 100
  69.         self.staticlabel_55 = tk.Label(master=self.window, text="mousedown tag:")
  70.         self.staticlabel_55.place(x=x_pos, y=y_pos)
  71.         x_pos = 180
  72.         y_pos = 100
  73.         self.staticlabel_59 = tk.Label(master=self.window, text="mouseup tag:")
  74.         self.staticlabel_59.place(x=x_pos, y=y_pos)
  75.  
  76.         x_pos = 20
  77.         y_pos = 130
  78.         self.label_tagdown = tk.Label(master=self.window, text="[none]")
  79.         self.label_tagdown.place(x=x_pos, y=y_pos)
  80.         x_pos = 180
  81.         y_pos = 130
  82.         self.label_tagup = tk.Label(master=self.window, text="[none]")
  83.         self.label_tagup.place(x=x_pos, y=y_pos)
  84.  
  85.         x_pos = 350
  86.         y_pos = 40
  87.         self.canvas1 = tk.Canvas(master=self.window, width=400, height=400)
  88.         self.canvas1.place(x=x_pos, y=y_pos)
  89.         self.canvas1.create_rectangle(2, 2, 400, 400, outline='black', fill='white')
  90.  
  91.         target_1 = self.canvas1.create_rectangle(80, 80, 180, 180, outline='black', fill='green', tags=('greenbox'))
  92.         self.canvas1.tag_bind('greenbox', '<Button-1>', partial(self.chart_item_mousedown_event, target_1))
  93.         self.canvas1.tag_bind('greenbox', '<ButtonRelease-1>', partial(self.chart_item_mouseup_event, target_1))      
  94.  
  95.         target_2 = self.canvas1.create_rectangle(240, 180, 340, 280, outline='black', fill='red', tags=('redbox'))
  96.         self.canvas1.tag_bind('redbox', '<Button-1>', partial(self.chart_item_mousedown_event, target_2))
  97.         self.canvas1.tag_bind('redbox', '<ButtonRelease-1>', partial(self.chart_item_mouseup_event, target_2))
  98.  
  99.  
  100.  
  101.     def chart_item_mousedown_event(self, c_handle, event):
  102.         temp_tag = self.canvas1.gettags(c_handle)
  103.         if temp_tag == "":
  104.             self.label_tagdown.config(text='[none]')
  105.         else:
  106.             self.label_tagdown.config(text=temp_tag)
  107.         m_event = "x:" + str(event.x) + ", y:" + str(event.y)
  108.         self.label_mousedown.config(text=m_event)
  109.  
  110.  
  111.  
  112.     def chart_item_mouseup_event(self, c_handle, event):
  113.         temp_tag = self.canvas1.gettags(c_handle)
  114.         if temp_tag == "":
  115.             self.label_tagup.config(text='[none]')
  116.         else:
  117.             self.label_tagup.config(text=temp_tag)
  118.         m_event = "x:" + str(event.x) + ", y:" + str(event.y)
  119.         self.label_mouseup.config(text=m_event)
  120.  
  121.  
  122.    
  123. if __name__ == "__main__":
  124.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement