Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
- # mouse-drag tester ver. 4.0
- '''
- This script is just three boxes that should left-click-drag on the canvas.
- The problem I get is that the GUI response starts out normal,
- but then lags more and more over time (with moving the boxes around) until it is nearly unusable.
- '''
- # @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
- import tkinter as tk
- from tkinter import ttk
- from tkinter import messagebox
- import math
- #import time
- #import _thread
- class drag_box_class1:
- def __init__(self, sWidth, sHeight, sXposition, sYposition):
- self.height = sHeight
- self.width = sWidth
- self.xPosition = sXposition
- self.yPosition = sYposition
- self.boxClicked = False
- self.x_clickOffset = 0
- self.y_clickOffset = 0
- self.snapToGrid = True
- def setMyColors(self, cRed, cGreen, cBlue):
- self.red = cRed
- self.green = cGreen
- self.blue = cBlue
- def displayMeNormal(self, drawingArea):
- this_color = '#%02x%02x%02x' % (self.red, self.green, self.blue)
- drawPoint1_x = self.xPosition - (self.width / 2) - self.x_clickOffset # this is the upper-left corner
- drawPoint1_y = self.yPosition - (self.height / 2) - self.y_clickOffset
- drawPoint2_x = self.xPosition - (self.width / 2) - self.x_clickOffset # this is the lower-left corner
- drawPoint2_y = self.yPosition + (self.height / 2) - self.y_clickOffset
- drawPoint3_x = self.xPosition + (self.width / 2) - self.x_clickOffset # this is the lower-right corner
- drawPoint3_y = self.yPosition + (self.height / 2) - self.y_clickOffset
- drawPoint4_x = self.xPosition + (self.width / 2) - self.x_clickOffset # this is the upper-left corner
- drawPoint4_y = self.yPosition - (self.height / 2) - self.y_clickOffset
- # left vertical line-
- drawingArea.create_line(drawPoint1_x, drawPoint1_y, drawPoint2_x, drawPoint2_y, fill=this_color)
- # lower horizontal line-
- drawingArea.create_line(drawPoint2_x, drawPoint2_y, drawPoint3_x, drawPoint3_y, fill=this_color)
- # right vertical line-
- drawingArea.create_line(drawPoint3_x, drawPoint3_y, drawPoint4_x, drawPoint4_y, fill=this_color)
- # upper horizontal line-
- drawingArea.create_line(drawPoint4_x, drawPoint4_y, drawPoint1_x, drawPoint1_y, fill=this_color)
- def unDrawMe(self, drawingArea):
- '''
- What this is:
- I tried adding and using functions to un-draw the canvas items instead of re-filling the whole canvas,
- but that didn't change the increasing lag problem.
- '''
- drawPoint1_x = self.xPosition - (self.width / 2) - self.x_clickOffset # this is the upper-left corner
- drawPoint1_y = self.yPosition - (self.height / 2) - self.y_clickOffset
- drawPoint2_x = self.xPosition - (self.width / 2) - self.x_clickOffset # this is the lower-left corner
- drawPoint2_y = self.yPosition + (self.height / 2) - self.y_clickOffset
- drawPoint3_x = self.xPosition + (self.width / 2) - self.x_clickOffset # this is the lower-right corner
- drawPoint3_y = self.yPosition + (self.height / 2) - self.y_clickOffset
- drawPoint4_x = self.xPosition + (self.width / 2) - self.x_clickOffset # this is the upper-left corner
- drawPoint4_y = self.yPosition - (self.height / 2) - self.y_clickOffset
- # left vertical line-
- drawingArea.create_line(drawPoint1_x, drawPoint1_y, drawPoint2_x, drawPoint2_y, fill='white')
- # lower horizontal line-
- drawingArea.create_line(drawPoint2_x, drawPoint2_y, drawPoint3_x, drawPoint3_y, fill='white')
- # right vertical line-
- drawingArea.create_line(drawPoint3_x, drawPoint3_y, drawPoint4_x, drawPoint4_y, fill='white')
- # upper horizontal line-
- drawingArea.create_line(drawPoint4_x, drawPoint4_y, drawPoint1_x, drawPoint1_y, fill='white')
- def checkForMouseDown(self, xLoc, yLoc):
- xPosition_minimum = self.xPosition - (self.width / 2)
- xPosition_maximum = self.xPosition + (self.width / 2)
- yPosition_minimum = self.yPosition - (self.height / 2)
- yPosition_maximum = self.yPosition + (self.height / 2)
- self.squareClicked = False
- if xLoc >= xPosition_minimum:
- if xLoc < xPosition_maximum:
- if yLoc >= yPosition_minimum:
- if yLoc < yPosition_maximum:
- #print("square method--click detected")
- self.x_clickOffset = xLoc - self.xPosition
- self.y_clickOffset = yLoc - self.yPosition
- self.boxClicked = True
- root = tk.Tk()
- root.title( "click-drag tester ver 4.0" )
- window_width = 1200
- window_height = 800
- window_position_x = 100
- window_position_y = 50
- root.geometry('%dx%d+%d+%d' % (window_width, window_height, window_position_x, window_position_y))
- def do_nothing():
- # this function does nothing
- # it is just to stick into buttons during GUI layout
- messagebox.showinfo("Doing nothing!", "do_nothing( nope! ) was called!")
- grid_left_margin = 20
- grid_top_margin = 30
- grid_column_width = 120
- grid_row_height = 40
- dragBox_xPosition = 50
- dragBox_yPosition = 50
- dragBox_width = grid_column_width - 20
- dragBox_height = grid_row_height - 10
- boxList1 = [] # this is the list of boxes to draw
- boxList1.append(drag_box_class1(dragBox_width, dragBox_height, 80, 100))
- boxList1[0].setMyColors(255, 0, 0)
- boxList1.append(drag_box_class1(dragBox_width, dragBox_height, 80, 200))
- boxList1[1].setMyColors(0, 127, 0)
- boxList1.append(drag_box_class1(dragBox_width, dragBox_height, 200, 200))
- boxList1[2].setMyColors(0, 0, 255)
- def draw_all_boxes():
- for thisBox in boxList1:
- thisBox.displayMeNormal(viewArea)
- def undraw_all_boxes():
- for thisBox in boxList1:
- thisBox.unDrawMe(viewArea)
- horizontalGridLines = []
- verticalGridLines = []
- def setup_horizontal_grid_lines():
- global grid_top_margin
- global horizontalGridLines
- global grid_row_height
- tempVal = grid_top_margin
- horizontalGridLines.append(tempVal)
- i = 1
- while i < 12:
- tempVal += grid_row_height
- horizontalGridLines.append(tempVal)
- i += 1
- setup_horizontal_grid_lines()
- def setup_vertical_grid_lines():
- global grid_left_margin
- global verticalGridLines
- global grid_column_width
- tempVal = grid_left_margin
- verticalGridLines.append(tempVal)
- i = 1
- while i < 8:
- tempVal += grid_column_width
- verticalGridLines.append(tempVal)
- i += 1
- setup_vertical_grid_lines()
- def refill_view_area():
- global viewArea
- global viewArea_width
- global viewArea_height
- viewArea.create_rectangle(0, 0, viewArea_width, viewArea_height, fill='white')
- gridColor1 = '#%02x%02x%02x' % (127, 127, 127)
- def draw_grid1():
- global viewArea
- global horizontalGridLines
- global verticalGridLines
- global gridColor1
- for rowPosition in horizontalGridLines:
- viewArea.create_line(0, rowPosition, 750, rowPosition, fill=gridColor1)
- for columnPosition in verticalGridLines:
- viewArea.create_line(columnPosition, 0, columnPosition, 480, fill=gridColor1)
- def undraw_grid1():
- global viewArea
- global horizontalGridLines
- global verticalGridLines
- global gridColor1
- for rowPosition in horizontalGridLines:
- viewArea.create_line(0, rowPosition, 750, rowPosition, fill='white')
- for columnPosition in verticalGridLines:
- viewArea.create_line(columnPosition, 0, columnPosition, 480, fill='white')
- root_xPosition = 50
- root_yPosition = 50
- displayFrame1_color = '#%02x%02x%02x' % (127, 127, 127)
- displayFrame1_width = 800
- displayFrame1_height = 500
- displayFrame1 = tk.Frame(master=root, width=displayFrame1_width, height=displayFrame1_height, borderwidth=2, relief=tk.GROOVE, bg=displayFrame1_color)
- displayFrame1.place(x=root_xPosition, y=root_yPosition)
- viewCanvas_sizeDifference = 16
- viewArea_width = displayFrame1_width - viewCanvas_sizeDifference
- viewArea_height = displayFrame1_height - viewCanvas_sizeDifference
- viewArea_xPosition = 4
- viewArea_yPosition = 4
- viewArea = tk.Canvas(master=displayFrame1, width=viewArea_width, height=viewArea_height, bg="white")
- viewArea.place(x=viewArea_xPosition, y=viewArea_yPosition)
- labelVspacer = 24
- labelcol1 = 900
- labelcol2 = 960
- xPosition = labelcol1
- yPosition = 200
- staticLabel1 = tk.Label(master=root, text="Mouse x:")
- staticLabel1.place(x=xPosition, y = yPosition)
- xPosition = labelcol2
- mousexLabel = tk.Label(master=root, text="[???]")
- mousexLabel.place(x=xPosition, y = yPosition)
- xPosition = labelcol1
- yPosition = yPosition + labelVspacer
- staticLabel1 = tk.Label(master=root, text="Mouse y:")
- staticLabel1.place(x=xPosition, y = yPosition)
- xPosition = labelcol2
- mouseyLabel = tk.Label(master=root, text="[???]")
- mouseyLabel.place(x=xPosition, y = yPosition)
- xPosition = labelcol1
- yPosition = yPosition + labelVspacer
- staticLabel1 = tk.Label(master=root, text="Box #:")
- staticLabel1.place(x=xPosition, y = yPosition)
- xPosition = labelcol2
- boxIDLabel = tk.Label(master=root, text="[???]")
- boxIDLabel.place(x=xPosition, y = yPosition)
- xPosition = labelcol1
- yPosition = yPosition + labelVspacer
- staticLabel1 = tk.Label(master=root, text="Box x:")
- staticLabel1.place(x=xPosition, y = yPosition)
- xPosition = labelcol2
- box_x_Label = tk.Label(master=root, text="[???]")
- box_x_Label.place(x=xPosition, y = yPosition)
- xPosition = labelcol1
- yPosition = yPosition + labelVspacer
- staticLabel1 = tk.Label(master=root, text="Box y:")
- staticLabel1.place(x=xPosition, y = yPosition)
- xPosition = labelcol2
- box_y_Label = tk.Label(master=root, text="[???]")
- box_y_Label.place(x=xPosition, y = yPosition)
- draw_all_boxes()
- draw_grid1()
- def show_box_info(thisEvent, thisBox, counter):
- mousexLabel.config(text=thisEvent.x)
- mouseyLabel.config(text=thisEvent.y)
- boxIDLabel.config(text=counter)
- box_x_Label.config(text=thisBox.xPosition)
- box_y_Label.config(text=thisBox.yPosition)
- boxDragging = False
- def check_all_boxes(event):
- global boxList1
- global boxDragging
- for thisBox in boxList1:
- thisBox.checkForMouseDown(event.x, event.y)
- if thisBox.boxClicked == True:
- boxDragging = True
- return
- viewArea.bind('<Button-1>', check_all_boxes)
- dragThreshold_horizontal = 2
- dragThreshold_vertical = 2
- redrawingDisplay = False
- def mouseOver_function(event):
- global boxDragging
- if boxDragging == True:
- global redrawingDisplay
- global viewArea
- global dragThreshold_horizontal
- global dragThreshold_vertical
- #print("Mouse position: (%s %s)" % (event.x, event.y))
- global viewArea
- global dragThreshold_horizontal
- global dragThreshold_vertical
- if redrawingDisplay == False:
- redrawingDisplay = True
- counter = 0
- for thisBox in boxList1:
- if thisBox.boxClicked == True:
- xDifference = event.x - thisBox.xPosition
- yDifference = event.y - thisBox.yPosition
- if abs(xDifference) > dragThreshold_horizontal or abs(yDifference) > dragThreshold_vertical:
- thisBox.xPosition = event.x
- thisBox.yPosition = event.y
- refill_view_area()
- draw_grid1()
- draw_all_boxes()
- show_box_info(event, thisBox, counter)
- #time.sleep(.05)
- counter += 1
- redrawingDisplay = False
- viewArea.bind('<B1-Motion>', mouseOver_function)
- def release_all_boxes(event):
- global boxList1
- global boxDragging
- for thisBox in boxList1:
- thisBox.boxClicked = False
- refill_view_area()
- draw_grid1()
- draw_all_boxes()
- boxDragging = False
- viewArea.bind('<ButtonRelease-1>', release_all_boxes)
- root.mainloop()
- # ~~~~~~~~~ the end ~~~~~~~~~
Advertisement
Add Comment
Please, Sign In to add comment