skizziks_53

GUI dragger v4

Dec 15th, 2019
743
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 12.16 KB | None | 0 0
  1.  
  2.  
  3. # @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  4. # mouse-drag tester ver. 4.0
  5. '''
  6. This script is just three boxes that should left-click-drag on the canvas.
  7.  
  8. The problem I get is that the GUI response starts out normal,
  9. but then lags more and more over time (with moving the boxes around) until it is nearly unusable.
  10. '''
  11. # @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  12.  
  13.  
  14. import tkinter as tk
  15. from tkinter import ttk
  16. from tkinter import messagebox
  17. import math
  18. #import time
  19. #import _thread
  20.  
  21.  
  22.  
  23.  
  24.  
  25.  
  26.  
  27.  
  28.  
  29. class drag_box_class1:
  30.     def __init__(self, sWidth, sHeight, sXposition, sYposition):
  31.         self.height = sHeight
  32.         self.width = sWidth
  33.         self.xPosition = sXposition
  34.         self.yPosition = sYposition
  35.         self.boxClicked = False
  36.         self.x_clickOffset = 0
  37.         self.y_clickOffset = 0
  38.         self.snapToGrid = True
  39.  
  40.     def setMyColors(self, cRed, cGreen, cBlue):
  41.         self.red = cRed
  42.         self.green = cGreen
  43.         self.blue = cBlue
  44.  
  45.     def displayMeNormal(self, drawingArea):
  46.         this_color = '#%02x%02x%02x' % (self.red, self.green, self.blue)
  47.         drawPoint1_x = self.xPosition - (self.width / 2) - self.x_clickOffset # this is the upper-left corner
  48.         drawPoint1_y = self.yPosition - (self.height / 2) - self.y_clickOffset
  49.         drawPoint2_x = self.xPosition - (self.width / 2) - self.x_clickOffset # this is the lower-left corner
  50.         drawPoint2_y = self.yPosition + (self.height / 2) - self.y_clickOffset
  51.         drawPoint3_x = self.xPosition + (self.width / 2) - self.x_clickOffset # this is the lower-right corner
  52.         drawPoint3_y = self.yPosition + (self.height / 2) - self.y_clickOffset
  53.         drawPoint4_x = self.xPosition + (self.width / 2) - self.x_clickOffset # this is the upper-left corner
  54.         drawPoint4_y = self.yPosition - (self.height / 2) - self.y_clickOffset
  55.         # left vertical line-
  56.         drawingArea.create_line(drawPoint1_x, drawPoint1_y, drawPoint2_x, drawPoint2_y, fill=this_color)
  57.         # lower horizontal line-
  58.         drawingArea.create_line(drawPoint2_x, drawPoint2_y, drawPoint3_x, drawPoint3_y, fill=this_color)
  59.         # right vertical line-
  60.         drawingArea.create_line(drawPoint3_x, drawPoint3_y, drawPoint4_x, drawPoint4_y, fill=this_color)
  61.         # upper horizontal line-
  62.         drawingArea.create_line(drawPoint4_x, drawPoint4_y, drawPoint1_x, drawPoint1_y, fill=this_color)
  63.  
  64.     def unDrawMe(self, drawingArea):
  65.         '''
  66.        What this is:
  67.        I tried adding and using functions to un-draw the canvas items instead of re-filling the whole canvas,
  68.        but that didn't change the increasing lag problem.
  69.        '''
  70.         drawPoint1_x = self.xPosition - (self.width / 2) - self.x_clickOffset # this is the upper-left corner
  71.         drawPoint1_y = self.yPosition - (self.height / 2) - self.y_clickOffset
  72.         drawPoint2_x = self.xPosition - (self.width / 2) - self.x_clickOffset # this is the lower-left corner
  73.         drawPoint2_y = self.yPosition + (self.height / 2) - self.y_clickOffset
  74.         drawPoint3_x = self.xPosition + (self.width / 2) - self.x_clickOffset # this is the lower-right corner
  75.         drawPoint3_y = self.yPosition + (self.height / 2) - self.y_clickOffset
  76.         drawPoint4_x = self.xPosition + (self.width / 2) - self.x_clickOffset # this is the upper-left corner
  77.         drawPoint4_y = self.yPosition - (self.height / 2) - self.y_clickOffset
  78.         # left vertical line-
  79.         drawingArea.create_line(drawPoint1_x, drawPoint1_y, drawPoint2_x, drawPoint2_y, fill='white')
  80.         # lower horizontal line-
  81.         drawingArea.create_line(drawPoint2_x, drawPoint2_y, drawPoint3_x, drawPoint3_y, fill='white')
  82.         # right vertical line-
  83.         drawingArea.create_line(drawPoint3_x, drawPoint3_y, drawPoint4_x, drawPoint4_y, fill='white')
  84.         # upper horizontal line-
  85.         drawingArea.create_line(drawPoint4_x, drawPoint4_y, drawPoint1_x, drawPoint1_y, fill='white')
  86.  
  87.     def checkForMouseDown(self, xLoc, yLoc):
  88.         xPosition_minimum = self.xPosition - (self.width / 2)
  89.         xPosition_maximum = self.xPosition + (self.width / 2)
  90.         yPosition_minimum = self.yPosition - (self.height / 2)
  91.         yPosition_maximum = self.yPosition + (self.height / 2)
  92.         self.squareClicked = False
  93.         if xLoc >= xPosition_minimum:
  94.             if xLoc < xPosition_maximum:
  95.                 if yLoc >= yPosition_minimum:
  96.                     if yLoc < yPosition_maximum:
  97.                         #print("square method--click detected")
  98.                         self.x_clickOffset = xLoc - self.xPosition
  99.                         self.y_clickOffset = yLoc - self.yPosition
  100.                         self.boxClicked = True
  101.  
  102.  
  103.  
  104.  
  105.  
  106.  
  107.  
  108.  
  109.  
  110.  
  111. root = tk.Tk()
  112. root.title( "click-drag tester ver 4.0" )
  113. window_width = 1200
  114. window_height = 800
  115. window_position_x = 100
  116. window_position_y = 50
  117. root.geometry('%dx%d+%d+%d' % (window_width, window_height, window_position_x, window_position_y))
  118.  
  119. def do_nothing():
  120.     # this function does nothing
  121.     # it is just to stick into buttons during GUI layout
  122.     messagebox.showinfo("Doing nothing!", "do_nothing( nope! ) was called!")
  123.  
  124.  
  125.  
  126. grid_left_margin = 20
  127. grid_top_margin = 30
  128. grid_column_width = 120
  129. grid_row_height = 40
  130.  
  131. dragBox_xPosition = 50
  132. dragBox_yPosition = 50
  133. dragBox_width = grid_column_width - 20
  134. dragBox_height = grid_row_height - 10
  135.  
  136.  
  137. boxList1 = [] # this is the list of boxes to draw
  138. boxList1.append(drag_box_class1(dragBox_width, dragBox_height, 80, 100))
  139. boxList1[0].setMyColors(255, 0, 0)
  140. boxList1.append(drag_box_class1(dragBox_width, dragBox_height, 80, 200))
  141. boxList1[1].setMyColors(0, 127, 0)
  142. boxList1.append(drag_box_class1(dragBox_width, dragBox_height, 200, 200))
  143. boxList1[2].setMyColors(0, 0, 255)
  144.  
  145. def draw_all_boxes():
  146.     for thisBox in boxList1:
  147.         thisBox.displayMeNormal(viewArea)
  148.  
  149. def undraw_all_boxes():
  150.     for thisBox in boxList1:
  151.         thisBox.unDrawMe(viewArea)
  152.  
  153.  
  154. horizontalGridLines = []
  155. verticalGridLines = []
  156.  
  157. def setup_horizontal_grid_lines():
  158.     global grid_top_margin
  159.     global horizontalGridLines
  160.     global grid_row_height
  161.     tempVal = grid_top_margin
  162.     horizontalGridLines.append(tempVal)
  163.     i = 1
  164.     while i < 12:
  165.         tempVal += grid_row_height
  166.         horizontalGridLines.append(tempVal)
  167.         i += 1
  168.  
  169. setup_horizontal_grid_lines()
  170.  
  171. def setup_vertical_grid_lines():
  172.     global grid_left_margin
  173.     global verticalGridLines
  174.     global grid_column_width
  175.     tempVal = grid_left_margin
  176.     verticalGridLines.append(tempVal)
  177.     i = 1
  178.     while i < 8:
  179.         tempVal += grid_column_width
  180.         verticalGridLines.append(tempVal)
  181.         i += 1
  182.  
  183. setup_vertical_grid_lines()
  184.  
  185. def refill_view_area():
  186.     global viewArea
  187.     global viewArea_width
  188.     global viewArea_height
  189.     viewArea.create_rectangle(0, 0, viewArea_width, viewArea_height, fill='white')
  190.  
  191. gridColor1 = '#%02x%02x%02x' % (127, 127, 127)
  192.  
  193. def draw_grid1():
  194.     global viewArea
  195.     global horizontalGridLines
  196.     global verticalGridLines
  197.     global gridColor1
  198.     for rowPosition in horizontalGridLines:
  199.         viewArea.create_line(0, rowPosition, 750, rowPosition, fill=gridColor1)
  200.     for columnPosition in verticalGridLines:
  201.         viewArea.create_line(columnPosition, 0, columnPosition, 480, fill=gridColor1)
  202.  
  203. def undraw_grid1():
  204.     global viewArea
  205.     global horizontalGridLines
  206.     global verticalGridLines
  207.     global gridColor1
  208.     for rowPosition in horizontalGridLines:
  209.         viewArea.create_line(0, rowPosition, 750, rowPosition, fill='white')
  210.     for columnPosition in verticalGridLines:
  211.         viewArea.create_line(columnPosition, 0, columnPosition, 480, fill='white')
  212.  
  213.  
  214.  
  215. root_xPosition = 50
  216. root_yPosition = 50
  217. displayFrame1_color = '#%02x%02x%02x' % (127, 127, 127)
  218. displayFrame1_width = 800
  219. displayFrame1_height = 500
  220. displayFrame1 = tk.Frame(master=root, width=displayFrame1_width, height=displayFrame1_height, borderwidth=2, relief=tk.GROOVE, bg=displayFrame1_color)
  221. displayFrame1.place(x=root_xPosition, y=root_yPosition)
  222.  
  223. viewCanvas_sizeDifference = 16
  224. viewArea_width = displayFrame1_width - viewCanvas_sizeDifference
  225. viewArea_height = displayFrame1_height - viewCanvas_sizeDifference
  226. viewArea_xPosition = 4
  227. viewArea_yPosition = 4
  228. viewArea = tk.Canvas(master=displayFrame1, width=viewArea_width, height=viewArea_height, bg="white")
  229. viewArea.place(x=viewArea_xPosition, y=viewArea_yPosition)
  230.  
  231. labelVspacer = 24
  232. labelcol1 = 900
  233. labelcol2 = 960
  234.  
  235. xPosition = labelcol1
  236. yPosition = 200
  237. staticLabel1 = tk.Label(master=root, text="Mouse x:")
  238. staticLabel1.place(x=xPosition, y = yPosition)
  239. xPosition = labelcol2
  240. mousexLabel = tk.Label(master=root, text="[???]")
  241. mousexLabel.place(x=xPosition, y = yPosition)
  242.  
  243. xPosition = labelcol1
  244. yPosition = yPosition + labelVspacer
  245. staticLabel1 = tk.Label(master=root, text="Mouse y:")
  246. staticLabel1.place(x=xPosition, y = yPosition)
  247. xPosition = labelcol2
  248. mouseyLabel = tk.Label(master=root, text="[???]")
  249. mouseyLabel.place(x=xPosition, y = yPosition)
  250.  
  251. xPosition = labelcol1
  252. yPosition = yPosition + labelVspacer
  253. staticLabel1 = tk.Label(master=root, text="Box #:")
  254. staticLabel1.place(x=xPosition, y = yPosition)
  255. xPosition = labelcol2
  256. boxIDLabel = tk.Label(master=root, text="[???]")
  257. boxIDLabel.place(x=xPosition, y = yPosition)
  258.  
  259. xPosition = labelcol1
  260. yPosition = yPosition + labelVspacer
  261. staticLabel1 = tk.Label(master=root, text="Box x:")
  262. staticLabel1.place(x=xPosition, y = yPosition)
  263. xPosition = labelcol2
  264. box_x_Label = tk.Label(master=root, text="[???]")
  265. box_x_Label.place(x=xPosition, y = yPosition)
  266.  
  267. xPosition = labelcol1
  268. yPosition = yPosition + labelVspacer
  269. staticLabel1 = tk.Label(master=root, text="Box y:")
  270. staticLabel1.place(x=xPosition, y = yPosition)
  271. xPosition = labelcol2
  272. box_y_Label = tk.Label(master=root, text="[???]")
  273. box_y_Label.place(x=xPosition, y = yPosition)
  274.  
  275.  
  276. draw_all_boxes()
  277.  
  278. draw_grid1()
  279.  
  280.  
  281. def show_box_info(thisEvent, thisBox, counter):
  282.     mousexLabel.config(text=thisEvent.x)
  283.     mouseyLabel.config(text=thisEvent.y)
  284.     boxIDLabel.config(text=counter)
  285.     box_x_Label.config(text=thisBox.xPosition)
  286.     box_y_Label.config(text=thisBox.yPosition)
  287.  
  288. boxDragging = False
  289.  
  290. def check_all_boxes(event):
  291.     global boxList1
  292.     global boxDragging
  293.     for thisBox in boxList1:
  294.         thisBox.checkForMouseDown(event.x, event.y)
  295.         if thisBox.boxClicked == True:
  296.             boxDragging = True
  297.             return
  298.  
  299. viewArea.bind('<Button-1>', check_all_boxes)
  300.  
  301.  
  302.  
  303.  
  304. dragThreshold_horizontal = 2
  305. dragThreshold_vertical = 2
  306. redrawingDisplay = False
  307.  
  308. def mouseOver_function(event):
  309.     global boxDragging
  310.     if boxDragging == True:
  311.         global redrawingDisplay
  312.         global viewArea
  313.         global dragThreshold_horizontal
  314.         global dragThreshold_vertical
  315.         #print("Mouse position: (%s %s)" % (event.x, event.y))
  316.         global viewArea
  317.         global dragThreshold_horizontal
  318.         global dragThreshold_vertical
  319.         if redrawingDisplay == False:
  320.             redrawingDisplay = True
  321.             counter = 0
  322.             for thisBox in boxList1:
  323.                 if thisBox.boxClicked == True:
  324.                     xDifference = event.x - thisBox.xPosition
  325.                     yDifference = event.y - thisBox.yPosition
  326.                     if abs(xDifference) > dragThreshold_horizontal or abs(yDifference) > dragThreshold_vertical:
  327.                         thisBox.xPosition = event.x
  328.                         thisBox.yPosition = event.y
  329.                         refill_view_area()
  330.                         draw_grid1()
  331.                         draw_all_boxes()
  332.                         show_box_info(event, thisBox, counter)
  333.                         #time.sleep(.05)
  334.                 counter += 1
  335.             redrawingDisplay = False
  336.  
  337. viewArea.bind('<B1-Motion>', mouseOver_function)
  338.  
  339.  
  340.  
  341. def release_all_boxes(event):
  342.     global boxList1
  343.     global boxDragging
  344.     for thisBox in boxList1:
  345.         thisBox.boxClicked = False
  346.     refill_view_area()
  347.     draw_grid1()
  348.     draw_all_boxes()
  349.     boxDragging = False
  350.  
  351. viewArea.bind('<ButtonRelease-1>', release_all_boxes)
  352.  
  353.  
  354.  
  355.  
  356.  
  357.  
  358. root.mainloop()
  359.  
  360.  
  361.  
  362. # ~~~~~~~~~ the end ~~~~~~~~~
Advertisement
Add Comment
Please, Sign In to add comment