MousePressEvent inside my Pyqt PlotWidget- Simulate event - python-3.x

This is full code base. Im trying to simulate a mouse click in the graph which is self.graph which is a plotwidget. I want the mouse click to happen in the plotrunnable class so afte I click the graph button it will automatically update and right before going to sleep it will simulate a click on the graph to make it seem like its automatically updating
# Form implementation generated from reading ui file 'U:\Embedded Graphics View2.ui'
#
# Created by: PyQt5 UI code generator 5.9.2
#
# WARNING! All changes made in this file will be lost!
from PyQt5 import QtCore, QtGui, QtWidgets
import pyqtgraph as pg
import pyodbc as db
import pandas as pd
import time, os
from time import strptime as strptime
from time import mktime as mktime
from time import gmtime, strftime
from pyqtgraph import AxisItem
from datetime import datetime, timedelta
from numpy import ceil
import numpy as np
import sip
os.environ['TZ']='EST'
now=time.asctime(time.localtime())
start_time=time.time()
print('starting at '+now)
def time(date):
if date == '':
return None
else:
datetime = date
pattern = '%Y%m%d %H:%M:%S'
epoch = int(mktime(strptime(datetime, pattern)))
return epoch
def connecttosql():
host='{SQL Server}'
server='SERVER' ######### DEV - WILL CHANGE TO PRODUCTION WHEN DELIVERED
database='DB'
username='user'
password='pass'
try:
cs= 'Driver=%s;Server=%s;Uid=%s;Pwd=%s;Database=%s;' % (host,server, username, password, database)
global conn #### THIS WILL BE USED ON THE QUERY
conn= db.connect(cs)
print ('Connected successfully to '+host+', '+server)
except Exception as e:
print ('Error: ' + str (e))
def testconnection():
cursor=conn.cursor()
try:
cursor.execute("SELECT VERSION()")
res=cursor.fetchone()
print(res)
ver=res[0]
if ver in None:
return False
else:
return True
except:
print ("ERROR IN CONNECTION")
return False
def closeconnection():
conn.close
print('CONNECTION WITH DATABASE HAS BEEN CLOSED')
def query(ticker,interval,ST,ET):
# target='U:/py/sql csv/'+ticker+' - '+interval+'.csv'
global conn
table = 'BAR_BB' ### hard code
qry = f"SELECT * FROM {table} WHERE SY = {ticker} AND IL = {interval} AND ST >= {ST} AND ST <= {ET}"
df=pd.read_sql(qry,conn) ###### format will change
df.set_index('ST',inplace=True)
#df.to_csv(target,encoding='utf-8',index=False)
st=df.index.tolist()
op=df['O'].tolist()
hi=df['H'].tolist()
lo=df['L'].tolist()
cl=df['C'].tolist()
bars=[]
x=0
for i in st:
bar=[st[x],op[x],cl[x],lo[x],hi[x]]
bars.append(bar)
x=x+1
data=bars
return data
##Picture that goes in the UI
class CandlestickItem(pg.GraphicsObject):
def __init__(self):
pg.GraphicsObject.__init__(self)
self.flagHasData = False
#QtCore.pyqtSlot(list)
def set_data(self, data):
self.data = data
self.flagHasData = True
self.generatePicture()
self.informViewBoundsChanged()
def generatePicture(self):
## pre-computing a QPicture object allows paint() to run much more quickly,
## rather than re-drawing the shapes every time.
self.picture = QtGui.QPicture()
p = QtGui.QPainter(self.picture)
p.setPen(pg.mkPen('w'))
w = (self.data[1][0] - self.data[0][0]) / 3.
for (t, open, close, min, max) in self.data:
p.drawLine(QtCore.QPointF(t, min), QtCore.QPointF(t, max))
if open > close:
p.setBrush(pg.mkBrush('r'))
else:
p.setBrush(pg.mkBrush('g'))
p.drawRect(QtCore.QRectF(t-w, open, w*2, close-open))
p.end()
def paint(self, p, *args):
p.drawPicture(0, 0, self.picture)
def boundingRect(self):
## boundingRect _must_ indicate the entire area that will be drawn on
## or else we will get artifacts and possibly crashing.
## (in this case, QPicture does all the work of computing the bouning rect for us)
return QtCore.QRectF(self.picture.boundingRect())
class DateAxisItem(AxisItem):
"""
A tool that provides a date-time aware axis. It is implemented as an
AxisItem that interpretes positions as unix timestamps (i.e. seconds
since 1970).
The labels and the tick positions are dynamically adjusted depending
on the range.
It provides a :meth:`attachToPlotItem` method to add it to a given
PlotItem
"""
# Max width in pixels reserved for each label in axis
_pxLabelWidth = 80
def __init__(self, *args, **kwargs):
AxisItem.__init__(self, *args, **kwargs)
self._oldAxis = None
def tickValues(self, minVal, maxVal, size):
"""
Reimplemented from PlotItem to adjust to the range and to force
the ticks at "round" positions in the context of time units instead of
rounding in a decimal base
"""
maxMajSteps = int(size/self._pxLabelWidth)
dt1 = datetime.fromtimestamp(minVal)
dt2 = datetime.fromtimestamp(maxVal)
dx = maxVal - minVal
majticks = []
if dx > 63072001: # 3600s*24*(365+366) = 2 years (count leap year)
d = timedelta(days=366)
for y in range(dt1.year + 1, dt2.year):
dt = datetime(year=y, month=1, day=1)
majticks.append(mktime(dt.timetuple()))
elif dx > 5270400: # 3600s*24*61 = 61 days
d = timedelta(days=31)
dt = dt1.replace(day=1, hour=0, minute=0,
second=0, microsecond=0) + d
while dt < dt2:
# make sure that we are on day 1 (even if always sum 31 days)
dt = dt.replace(day=1)
majticks.append(mktime(dt.timetuple()))
dt += d
elif dx > 172800: # 3600s24*2 = 2 days
d = timedelta(days=1)
dt = dt1.replace(hour=0, minute=0, second=0, microsecond=0) + d
while dt < dt2:
majticks.append(mktime(dt.timetuple()))
dt += d
elif dx > 7200: # 3600s*2 = 2hours
d = timedelta(hours=1)
dt = dt1.replace(minute=0, second=0, microsecond=0) + d
while dt < dt2:
majticks.append(mktime(dt.timetuple()))
dt += d
elif dx > 1200: # 60s*20 = 20 minutes
d = timedelta(minutes=10)
dt = dt1.replace(minute=(dt1.minute // 10) * 10,
second=0, microsecond=0) + d
while dt < dt2:
majticks.append(mktime(dt.timetuple()))
dt += d
elif dx > 120: # 60s*2 = 2 minutes
d = timedelta(minutes=1)
dt = dt1.replace(second=0, microsecond=0) + d
while dt < dt2:
majticks.append(mktime(dt.timetuple()))
dt += d
elif dx > 20: # 20s
d = timedelta(seconds=10)
dt = dt1.replace(second=(dt1.second // 10) * 10, microsecond=0) + d
while dt < dt2:
majticks.append(mktime(dt.timetuple()))
dt += d
elif dx > 2: # 2s
d = timedelta(seconds=1)
majticks = range(int(minVal), int(maxVal))
else: # <2s , use standard implementation from parent
return AxisItem.tickValues(self, minVal, maxVal, size)
L = len(majticks)
if L > maxMajSteps:
majticks = majticks[::int(ceil(float(L) / maxMajSteps))]
return [(d.total_seconds(), majticks)]
def tickStrings(self, values, scale, spacing):
"""Reimplemented from PlotItem to adjust to the range"""
ret = []
if not values:
return []
if spacing >= 31622400: # 366 days
fmt = "%Y"
elif spacing >= 2678400: # 31 days
fmt = "%Y %b"
elif spacing >= 86400: # = 1 day
fmt = "%b/%d"
elif spacing >= 3600: # 1 h
fmt = "%b/%d-%Hh"
elif spacing >= 60: # 1 m
fmt = "%H:%M"
elif spacing >= 1: # 1s
fmt = "%H:%M:%S"
else:
# less than 2s (show microseconds)
# fmt = '%S.%f"'
fmt = '[+%fms]' # explicitly relative to last second
for x in values:
try:
t = datetime.fromtimestamp(x)
ret.append(t.strftime(fmt))
except ValueError: # Windows can't handle dates before 1970
ret.append('')
return ret
def attachToPlotItem(self, plotItem):
"""Add this axis to the given PlotItem
:param plotItem: (PlotItem)
"""
self.setParentItem(plotItem)
viewBox = plotItem.getViewBox()
self.linkToView(viewBox)
self._oldAxis = plotItem.axes[self.orientation]['item']
self._oldAxis.hide()
plotItem.axes[self.orientation]['item'] = self
pos = plotItem.axes[self.orientation]['pos']
plotItem.layout.addItem(self, *pos)
self.setZValue(-1000)
def detachFromPlotItem(self):
"""Remove this axis from its attached PlotItem
(not yet implemented)
"""
raise NotImplementedError() # TODO
class TimeAxisItem(pg.AxisItem):
def tickStrings(self, values, scale, spacing):
return [datetime.fromtimestamp(value) for value in values]
##UI SetUP
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(1442, 1018)
MainWindow.setAutoFillBackground(False)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.graphicsView = QtWidgets.QWidget(self.centralwidget)
self.flagHasData = False
self.graphicsView.setGeometry(QtCore.QRect(0, 0, 1201, 991))
font = QtGui.QFont()
font.setPointSize(18)
self.graphicsView.setFont(font)
brush = QtGui.QBrush(QtGui.QColor(0, 0, 0))
brush.setStyle(QtCore.Qt.SolidPattern)
self.graphicsView.setObjectName("graphicsView")
self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit.setEnabled(True)
self.lineEdit.setGeometry(QtCore.QRect(1280, 20, 151, 21))
font = QtGui.QFont()
font.setPointSize(11)
self.lineEdit.setFont(font)
self.lineEdit.setObjectName("lineEdit")
self.lineEdit_2 = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit_2.setGeometry(QtCore.QRect(1280, 80, 151, 20))
font = QtGui.QFont()
font.setPointSize(11)
self.lineEdit_2.setFont(font)
self.lineEdit_2.setText("")
self.lineEdit_2.setObjectName("lineEdit_2")
self.lineEdit_3 = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit_3.setGeometry(QtCore.QRect(1280, 160, 151, 20))
font = QtGui.QFont()
font.setPointSize(11)
self.lineEdit_3.setFont(font)
self.lineEdit_3.setObjectName("lineEdit_3")
self.lineEdit_4 = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit_4.setGeometry(QtCore.QRect(1280, 230, 151, 20))
font = QtGui.QFont()
font.setPointSize(11)
self.lineEdit_4.setFont(font)
self.lineEdit_4.setObjectName("lineEdit_4")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(1280, 0, 91, 16))
self.label.setObjectName("label")
self.label_2 = QtWidgets.QLabel(self.centralwidget)
self.label_2.setGeometry(QtCore.QRect(1280, 60, 111, 16))
self.label_2.setObjectName("label_2")
self.label_3 = QtWidgets.QLabel(self.centralwidget)
self.label_3.setGeometry(QtCore.QRect(1280, 120, 51, 16))
self.label_3.setObjectName("label_3")
self.label_4 = QtWidgets.QLabel(self.centralwidget)
self.label_4.setGeometry(QtCore.QRect(1280, 190, 101, 16))
self.label_4.setObjectName("label_4")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(1280, 260, 75, 23))
self.pushButton.setObjectName("pushButton")
self.label_5 = QtWidgets.QLabel(self.centralwidget)
self.label_5.setGeometry(QtCore.QRect(1280, 140, 170, 20))
self.label_5.setObjectName("label_5")
self.label_6 = QtWidgets.QLabel(self.centralwidget)
self.label_6.setGeometry(QtCore.QRect(1280, 210, 170, 20))
self.label_6.setObjectName("label_6")
self.pushButton_2 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_2.setGeometry(QtCore.QRect(1360, 260, 75, 23))
self.pushButton_2.setObjectName("pushButton_2")
MainWindow.setCentralWidget(self.centralwidget)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.pushButton.clicked.connect(self.btn_click)
self.pushButton_2.clicked.connect(self.clear)
self.main_layout = QtWidgets.QHBoxLayout()
self.graphicsView.setLayout(self.main_layout)
self.graph = None
self.glayout = None
self.data = []
self.vb = None
self.main_layout.addWidget(self.graph)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
##renaming all the Labels and Window
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "Graph Interface"))
self.label.setText(_translate("MainWindow", "Ticker (Bloomberg)"))
self.label_2.setText(_translate("MainWindow", "Interval (In Seconds)"))
self.label_3.setText(_translate("MainWindow", "Start Time"))
self.label_4.setText(_translate("MainWindow", "End Time (Optional)"))
self.pushButton.setText(_translate("MainWindow", "Graph"))
self.label_5.setText(_translate("MainWindow", "YYYYMMDD 00:00:00 (UTC)"))
self.label_6.setText(_translate("MainWindow", "YYYYMMDD 00:00:00 (UTC)"))
self.pushButton_2.setText(_translate("MainWindow", "Clear"))
global mouse_click_event
def mouse_click_event(self):
graph.clicked()
#Button Click function.
def btn_click(self):
global ticker, interval, start_time, end_time, graph
global DRAW_GRAPH
connecttosql()
self.data = [ ## fields are (time, open, close, min, max).
(1., 10, 13, 5, 15),
(2., 13, 17, 9, 20),
(3., 17, 14, 11, 23),
(4., 14, 15, 5, 19),
(5., 15, 9, 8, 22),
(6., 9, 15, 8, 16)]
self.graph = pg.PlotWidget(name = 'Whatever', aixsItems = {'bottom' : TimeAxisItem(orientation = 'bottom')})
# self.ticker = self.lineEdit.text()
#self.interval = self.lineEdit_2.text()
#self.start_time = time(self.lineEdit_3.text())
#self.end_time = time(self.lineEdit_4.text())
# if self.end_time == None:
# self.end_time = time(strftime("%Y%m%d %H:%M:%S", gmtime()))
# else:
# self.end_time = time((self.lineEdit_4.text()))
# self.ticker = "'{}'".format(self.ticker)
# self.interval = "'{}'".format(self.interval)
# ticker = self.ticker
# interval = self.interval
# start_time = self.start_time
# end_time = self.end_time
self.glayout = pg.GraphicsLayout()
self.vb = self.glayout.addViewBox()
self.vb.enableAutoRange(axis='xy',enable = True)
#self.data = query(self.ticker,self.interval,self.start_time,self.end_time)
self.item = CandlestickItem()
self.item.set_data(self.data)
self.graph.addItem(self.item)
self.axis1 = DateAxisItem(orientation = 'bottom')
self.axis1.attachToPlotItem(self.graph.getPlotItem())
self.graph.showGrid(y=True, alpha = 0.8)
self.main_layout.addWidget(self.graph)
runnable = PlotRunnable(self.item)
QtCore.QThreadPool.globalInstance().start(runnable)
def clear(self):
self.parent = self.graph.parent()
self.graph.setParent(None)
self.graph.setParent(self.parent)
class PlotRunnable(QtCore.QRunnable):
def __init__(self, it):
QtCore.QRunnable.__init__(self)
self.it = it
def run(self):
while True:
data = self.it.data
# end_time = end_time = time(strftime("%Y%m%d %H:%M:%S", gmtime()))
rand =[ ## fields are (time, open, close, min, max).
(1., 10, 13, 5, 15),
(2., 13, 17, 9, 20),
(3., 17, 14, 11, 23),
(4., 14, 15, 5, 19),
(5., 15, 9, 8, 22),
(6., 9, 15, 8, 16),
(7., 8, 16, 10, 17)]
new_bar = rand[-1]
data.append(new_bar)
QtCore.QMetaObject.invokeMethod(self.it, "set_data",
QtCore.Qt.QueuedConnection,
QtCore.Q_ARG(list, data))
#put mouse click here
print("ran")
#interval = int("{}".format(interval))
QtCore.QThread.msleep(1000)
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
if __name__ == '__main__':
import sys
if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):
QtGui.QApplication.instance().exec_()

Related

How to reference an attribute from one class using another class

Here is my code
import random
class Player_Character:
def __init__(self, hp, maxhp, ac, THAC0):
self.hp = int(hp)
self.maxhp = int(maxhp)
self.ac = int(ac)
self.THAC0 = int(THAC0)
def attack(goblin):
Player_Character_Damage = random.randint(1,10)
goblin.hp -= Player_Character_Damage
if (goblin.hp <= 0):
print("congratulations you killed the goblin")
del goblin
class goblin:
def __init__(self, hp, maxhp, ac, THAC0):
self.hp = int(hp)
self.maxhp = int(maxhp)
self.ac = int(ac)
self.THAC0 = int(THAC0)
def attack(Player_Character):
goblin_damage = random.randint(1,4)
Player_Character.hp -= goblin_damage
if (Player_Character.hp <= 0):
print("oh dear you have died")
del Player_Character
Player_Character(10, 20, 10, 15)
goblin(5, 10, 8, 18)
Player_Character.attack(goblin)
I expect Player_Character.attack(goblin) to do 1-10 damage to the goblin (probably killing it) but instead I get an error: AttributeError: type object 'goblin' has no attribute 'hp'... obviously I created the class with a 'hp' attribute and am now confused.
You have to instantiate objects
as #ShadowRanger explained in the comments, I should have mentioned that I added self to functions
import random
class Player_Character:
def __init__(self, hp, maxhp, ac, THAC0):
self.hp = int(hp)
self.maxhp = int(maxhp)
self.ac = int(ac)
self.THAC0 = int(THAC0)
def attack(self, goblin):
Player_Character_Damage = random.randint(1,10)
goblin.hp -= Player_Character_Damage
if (goblin.hp <= 0):
print("congratulations you killed the goblin")
del goblin
class goblin:
def __init__(self, hp, maxhp, ac, THAC0):
self.hp = int(hp)
self.maxhp = int(maxhp)
self.ac = int(ac)
self.THAC0 = int(THAC0)
def attack(self, Player_Character):
goblin_damage = random.randint(1,4)
Player_Character.hp -= goblin_damage
if (Player_Character.hp <= 0):
print("oh dear you have died")
del Player_Character
p = Player_Character(10, 20, 10, 15)
g = goblin(5, 10, 8, 18)
p.attack(g)
also #ShadowRanger mentioned that del does not work as intended, you can use it outside to delete instances.
p = Player_Character(10, 20, 10, 15)
g = goblin(5, 10, 8, 18)
p.attack(g)
if(g.hp <= 0):
print("congratulations you killed the goblin")
del g

local variable referenced before assignment - Surely not this difficult

I'm trying to write a script to control movements of a camera. I'm writing it in Thonny on a Raspberry Pi. I receive the following error when trying to move the camera using the GUI buttons. I've asked around and tried troubleshooting the problem but no luck yet.
if anyone can help that'd be awesome.
I appreciate there are alternative workflows to this script but it's me combining an old script with a new GUI so I'd love to see if they could be merged. If not oh well, I'll implement the other workflow.
[Here is the error message from Thonny][1]
from tkinter import *
import RPi.GPIO as GPIO
import time
import datetime, sys, tty, termios
#defining root
root = Tk()
root.title('Operation Little Brother V02')
#setting GPIO board
GPIO.setmode(GPIO.BOARD)
GPIO.setup(3,GPIO.OUT)
servo1 = GPIO.PWM(3, 50)
GPIO.setup(5,GPIO.OUT)
servo2 = GPIO.PWM(5, 50)
#setting servos to off
servo1.start(0)
servo2.start(0)
#user input section
dummy = input('Initiate project. Press Enter')
#setting servos to 90degrees/startup sequence
print('launching start up sequence')
servo1.ChangeDutyCycle(6)
time.sleep(1)
servo1.ChangeDutyCycle(0)
time.sleep(1)
print ('servo 1 test complete')
servo2.ChangeDutyCycle(6)
time.sleep(1)
servo2.ChangeDutyCycle(0)
print ('servo 2 test complete')
# Defining Functions
#x axis movement concept
def x_movement(x_dir):
if x_axis <= limit_x_left and x_axis >= limit_x_right:
x_axis = x_axis + x_dir
servo1.ChangeDutyCycle(x_axis)
time.sleep(0.3)
servo1.ChangeDutyCycle(0)
time.sleep(0.5)
print ('moved camera to position: ' +str(x_axis) + '/' +str(limit_x_left))
return
else:
print ('limits reached')
#y axis movement concept
def y_movement(y_dir):
if y_axis <= limit_y_down and y_axis >= limit_y_up:
y_axis = y_axis + y_dir
servo2.ChangeDutyCycle(y_axis)
time.sleep(0.3)
servo2.ChangeDutyCycle(0)
time.sleep(0.5)
print('moved camera to position: ' +str(y_axis) + '/' +str(limit_y_up))
return y_axis
else:
print ('limits reached')
#centre movement
def centre_movement():
y_axis = 6
servo2.ChangeDutyCycle(6)
servo2.ChangeDutyCycle(0)
time.sleep(0.5)
x_axis = 6
servo1.ChangeDutyCycle(6)
servo1.ChangeDutyCycle(0)
time.sleep(0.5)
print('camera centered to position 6')
return
#off sequence
def off():
servo1.ChangeDutyCycle(0)
servo2.ChangeDutyCycle(0)
servo1.stop()
servo2.stop()
GPIO.cleanup()
print('Whoopty fucking doo it\'s finished')
exit()
#defining limits for x and y axis
limit_x_left = 9
limit_x_right = 3
limit_y_up = 3
limit_y_down = 9
x_axis = 6
y_axis = 6
#frame creation
frame1 = LabelFrame(root, text = 'Welcome to Operation Little Brother', padx = 25, pady = 25, border = 5, fg = '#ffffff', bg = '#3e556b')
#button creation
button1_left = Button(frame1, text = 'Left', padx = 20, pady = 20, fg = 'purple', bg = '#63778a', command = lambda: x_movement(1))
button2_right = Button(frame1, text = 'Right', padx = 20, pady = 20, fg = 'orange', bg = '#63778a', command = lambda: x_movement(-1))
button3_up = Button(frame1, text = 'Up', padx = 20, pady = 20, fg = 'green', command = lambda: y_movement(1))
button4_down = Button(frame1, text = 'Down', padx = 20, pady = 20, fg = 'blue', command = lambda: y_movement(-1))
button5_centre = Button(frame1, text = 'Centre', padx = 20, pady = 20, fg = 'black', command = lambda: centre_movement())
button6_start = Button(frame1, text = 'Start', padx = 10, pady = 10, fg = 'green')
button7_stop = Button(frame1, text = 'Stop', padx = 10, pady = 10, fg = 'red', command = lambda: off())
#positioning the frame
frame1.pack()
#button positioning
button1_left.grid(row = 3, column = 0,)
button2_right.grid(row = 3, column = 2,)
button3_up.grid(row = 2, column = 1)
button4_down.grid(row = 4, column = 1)
button5_centre.grid(row = 3, column = 1)
button6_start.grid(row = 1, column = 0)
button7_stop.grid(row = 1, column = 2)
#Text formatting
button1_left.configure(font='Calibri 15')
button2_right.configure(font='Calibri 15')
button3_up.configure(font='Calibri 15')
button4_down.configure(font='Calibri 15')
button5_centre.configure(font='Calibri 15')
button6_start.configure(font='Calibri 15')
button7_stop.configure(font='Calibri 15')
# Window Loop
root.mainloop()
[1]: https://i.stack.imgur.com/xIc0O.png

How to check if two objects touch each other in python tkinter?

I'm trying to create a function that tells me if two canvas objects(in my code, rec and block) touch each other in Tkinter. I tried to do so with the information of their coordinates but it doesn't seem to work. Can you please help me?
The function will be used in snake which I'm creating in the code below, so don't ask my why the code is so long.
The function is in the line 119, same_poz
This is my code:
from tkinter import *
import time
import random
import threading
root = Tk()
root.title('Snake')
x, y = 484, 484
recX, recY = x // 2, y // 2
recW, recH = recX + 22, recY + 22
randoms = []
for i in range(484):
if i % 11 == 0:
randoms.append(i)
blockX, blockY = random.choice(randoms), random.choice(randoms)
blockW, blockH = blockX + 22, blockY + 22
c = Canvas(root, bg='black', width=x, height=y)
c.pack()
class Snake(threading.Thread):
def __init__(self, c, x, y, recX, recY, recW, recH, blockX, blockY, blockW, blockH):
super(Snake, self).__init__()
self.c = c
self.x = x
self.y = y
self.recX = recX
self.recY = recY
self.recW = recW
self.recH = recH
self.blockW = blockW
self.blockH = blockH
self.rec = c.create_rectangle(recX, recY, recW, recH, fill='red', outline='white')
self.blockX = blockX
self.blockY = blockY
self.block = c.create_rectangle(blockX, blockY, blockW, blockH, fill='green',
outline='white')
self.moving_right = False
self.moving_left = False
self.moving_up = False
self.moving_down = False
self.moving = False
def movingright(self):
self.moving_right = True
self.moving_left = False
self.moving_up = False
self.moving_down = False
self.moving = True
c.move(self.rec, 11, 0)
self.after4 = root.after(150, self.movingright)
def movingleft(self):
self.moving_left = True
self.moving_left = False
self.moving_up = False
self.moving_down = False
self.moving = True
c.move(self.rec, -11, 0)
self.after3 = root.after(150, self.movingleft)
def movingup(self):
self.moving_up = True
self.moving_right = False
self.moving_left = False
self.moving_up = False
self.moving = True
c.move(self.rec, 0, -11)
self.after = root.after(150, self.movingup)
def movingdown(self):
self.moving_down = True
self.moving_right = False
self.moving_left = False
self.moving_down = False
self.moving = True
c.move(self.rec, 0, 11)
self.after2 = root.after(150, self.movingdown)
def stop(self):
self.moving_right = False
self.moving_left = False
self.moving_up = False
self.moving_down = False
self.moving = False
try:
root.after_cancel(self.after)
except AttributeError:
pass
try:
root.after_cancel(self.after2)
except AttributeError:
pass
try:
root.after_cancel(self.after3)
except AttributeError:
pass
try:
root.after_cancel(self.after4)
except AttributeError:
pass
def move(self, n):
if n.keysym == 'Up':
self.stop()
self.movingup()
if n.keysym == 'Down':
self.stop()
self.movingdown()
if n.keysym == 'Right':
self.stop()
self.movingright()
if n.keysym == 'Left':
self.stop()
self.movingleft()
def same_poz(self):
if self.blockY == self.recY:
self.helpY = random.randint(10, self.y - self.blockY)
self.c.move(self.block, 0, self.helpY)
if self.blockX == self.recY:
self.helpX = random.randint(10, self.x - self.blockX)
self.c.move(self.block, 0, self.helpX)
if self.blockW == self.recW:
self.helpW = random.randint(10, self.x - self.blockW)
self.c.move(self.block, 0, self.helpW)
if self.blockH == self.recH:
self.helpH = random.randint(10, self.y - self.blockH)
self.c.move(self.block, 0, helpH)
cube = Snake(c, x, y, recX, recY, recW, recH, blockX, blockY, blockW, blockH)
cube.start()
cube.c.bind_all('<Key>', cube.move, cube.stop)
cube.c.bind_all('<Key>', cube.moving_left, cube.moving_right)
cube.c.bind_all('<Key', cube.moving_up, cube.moving_down)
cube.c.bind(cube.same_poz)
root.mainloop()
There are way too many problems with your code, with what you provided. I do not know why you are programming using Tkinter when you are still lacking the basics of Python. (Don't mean to sound harsh)
I do not know why you decided to inherit from threading.Thread when
you do not have any use for anything Thread provides in that
class.
Way too many instance parameters which you have no use for at all, and too many instance parameters that you have to change every single time.
Not using elif and instead using if constantly.
If you are using a thread to handle Tkinter changes outside of mainloop, there is no need to use .after since it basically does that.
Tkinter has dedicated bindings for every single key, including combinations of keys. There is no need to catch every single key event.
Use if __name__ == '__main__': if you are working with a single script, for testing it.
Some reading material on Tkinter - http://effbot.org/tkinterbook/
Here is a minimal rework of the code, that is working.
import time
import random
import threading
from tkinter import *
MOVINGUP = 'u'
MOVINGDOWN = 'd'
MOVINGLEFT = 'l'
MOVINGRIGHT = 'r'
NOTMOVING = '0'
class Snake:
def __init__(self, root, recX, recY, recW, recH, blockX, blockY, blockW, blockH):
self.root = root
self.c = Canvas(root, bg='black', width=x, height=y)
self.c.pack()
self.rec = self.c.create_rectangle(recX, recY, recW, recH, fill='red', outline='white')
self.block = self.c.create_rectangle(blockX, blockY, blockW, blockH, fill='green', outline='white')
self.direction = NOTMOVING
self.root.bind('<Up>', lambda e: self.moveset(MOVINGUP))
self.root.bind('<Down>', lambda e: self.moveset(MOVINGDOWN))
self.root.bind('<Left>', lambda e: self.moveset(MOVINGLEFT))
self.root.bind('<Right>', lambda e: self.moveset(MOVINGRIGHT))
def moveset(self, direction):
self.direction = direction
def movement(self):
if self.direction == MOVINGUP:
self.c.move(self.rec, 0, -11)
elif self.direction == MOVINGDOWN:
self.c.move(self.rec, 0, 11)
elif self.direction == MOVINGLEFT:
self.c.move(self.rec, -11, 0)
elif self.direction == MOVINGRIGHT:
self.c.move(self.rec, 11, 0)
self.same_poz()
def run(self):
while True:
time.sleep(0.15)
self.movement()
def same_poz(self):
# Snake (x0, y0, x1, y1)
snakepos = self.c.bbox(self.rec)
# Food block (x0, y0, x1, y1)
food = self.c.bbox(self.block)
# If direction matters, if not then possible to only use self.hit in a single condition.
if self.direction == MOVINGRIGHT and self.hit(snakepos, food):
print('Caught the food moving right.')
elif self.direction == MOVINGLEFT and self.hit(snakepos, food):
print('Caught the food moving left.')
elif self.direction == MOVINGUP and self.hit(snakepos, food):
print('Caught the food moving up.')
elif self.direction == MOVINGDOWN and self.hit(snakepos, food):
print('Caught the food moving down.')
def hit(self, snakepos, food):
"""
Recieves coordinates of food block and snake block and returns if they collide.
:param snakepos: Tuple containing (x0, y0, x1, y1) of the snake.
:param food: Tuple containing (x0, y0, x1, y1) of the food block.
:return: Boolean whether they collide
"""
snakex = (snakepos[0], snakepos[2])
snakey = (snakepos[1], snakepos[3])
foodx = (food[0], food[2])
foody = (food[1], food[3])
# Returns True if any of the snake x cooridnates are between the food x coordinates, or both x coordinates match.
if any((foodx[0] < xcoord < foodx[1] for xcoord in snakex)) or foodx == snakex:
# Returns True if any of the snake y cooridnates are between the food y coordinates, or both y coordinates match.
return any((foody[0] < ycoord < foody[1] for ycoord in snakey)) or foody == snakey
return False
if __name__ == '__main__':
root = Tk()
root.title('Snake')
x, y = 484, 484
recX, recY = x // 2, y // 2
recW, recH = recX + 22, recY + 22
randoms = []
for i in range(484):
if i % 11 == 0:
randoms.append(i)
blockX, blockY = random.choice(randoms), random.choice(randoms)
blockW, blockH = blockX + 22, blockY + 22
snake = Snake(root, recX, recY, recW, recH, blockX, blockY, blockW, blockH)
threading.Thread(target=snake.run, daemon=True).start()
root.mainloop()

Issue with pygame.transform.rotate(), Index is apparently out of range

So I am working on a basic PacMan game, and am trying to get my player to run through its sprite_sheet to show an animation. It works flawlessly when I am moving it right (thats the original orientation of the image), however for any of the other directions where I pygame.transform.rotate() the image, it returns the following:
self.image = (PAC_UP[self.spritesheet_index % 4])
IndexError: list index out of range
Not sure why this is happening (clearly it has to do with rotation). Any explanation on rotations and why this is happening would be greatly appreciated!
Thanks!
Heres my code:
class sprite_sheet():
def __init__ (self, filename):
self.spritesheet = pygame.image.load(filename)
def get_image(self, x, y, width, height):
image = pygame.Surface((width, height))
image.set_colorkey(BLACK)
image.blit(self.spritesheet, (0,0), (x, y, width, height))
return image
PAC_RIGHT = []
for x in range(4):
image = (sprite_sheet('pacman-spritesheet.png').get_image(x*24, 0, 24, 24))
PAC_RIGHT.append(image)
PAC_UP = []
for x in range(4):
image = sprite_sheet('pacman-spritesheet.png').get_image(x*24, 0, 24, 24)
PAC_RIGHT.append(pygame.transform.rotate(image, 90))
PAC_LEFT = []
for x in range(4):
image = sprite_sheet('pacman-spritesheet.png').get_image(x*24, 0, 24, 24)
PAC_RIGHT.append(pygame.transform.rotate(image, 180))
PAC_DOWN = []
for x in range(4):
image = sprite_sheet('pacman-spritesheet.png').get_image(x*24, 0, 24, 24)
PAC_RIGHT.append(pygame.transform.rotate(image, 270))
class Player (pygame.sprite.Sprite):
def __init__(self, game, x, y):
self.groups = game.all_sprites
pygame.sprite.Sprite.__init__(self, self.groups)
self.game = game
self.image = sprite_sheet('pacman-spritesheet.png').get_image(0, 0, 24, 24)
self.x = x * TILESIZE
self.y = y * TILESIZE
self.rect = self.image.get_rect()
self.vx, self.vy = 0, 0
self.spritesheet_index = 0
self.first_time = pygame.time.get_ticks()
self.delay = 43
def get_keys(self):
self.vx, self.vy = 0, 0
keys = pygame.key.get_pressed()
self.spritesheet_index += 1
if keys[pygame.K_LEFT]:
self.vx = -PLAYER_SPEED
self.second_time = pygame.time.get_ticks()
if self.second_time-self.first_time > self.delay:
self.image = (PAC_LEFT[self.spritesheet_index % 4])
self.first_time = pygame.time.get_ticks()
if keys[pygame.K_RIGHT]:
self.vx = PLAYER_SPEED
self.second_time = pygame.time.get_ticks()
if self.second_time-self.first_time > self.delay:
self.image = (PAC_RIGHT[self.spritesheet_index % 4])
self.first_time = pygame.time.get_ticks()
if keys[pygame.K_UP]:
self.vy = -PLAYER_SPEED
self.second_time = pygame.time.get_ticks()
if self.second_time-self.first_time > self.delay:
self.image = (PAC_UP[self.spritesheet_index % 4])
self.first_time = pygame.time.get_ticks()
if keys[pygame.K_DOWN]:
self.vy = PLAYER_SPEED
self.second_time = pygame.time.get_ticks()
if self.second_time - self.first_time > self.delay:
self.image = (PAC_DOWN[self.spritesheet_index % 4])
self.first_time = pygame.time.get_ticks()
Looks like you're only adding your sprite images to the PAC_UP list:
PAC_RIGHT = []
for x in range(4):
image = (sprite_sheet('pacman-spritesheet.png').get_image(x*24, 0, 24, 24))
PAC_RIGHT.append(image)
PAC_UP = []
for x in range(4):
image = sprite_sheet('pacman-spritesheet.png').get_image(x*24, 0, 24, 24)
PAC_RIGHT.append(pygame.transform.rotate(image, 90))
Modify your code to append to the appropriate lists, PAC_DOWN, PAC_LEFT & PAC_RIGHT.
Additionally, this looks to be a copy-paste error. Instead of copy-pasting, it would be better to create a function to do your image loading.
Here's an untested example:
def load_sprite_images(rotation, filename='pacman-spritesheet.png'):
"""Load four sprite images from the specified file"""
images = []
for x in range(4):
image = (sprite_sheet(filename).get_image(x*24, 0, 24, 24))
images.append(pygame.transform.rotate(image, rotation))
return images
PAC_RIGHT = load_sprite_images(0)
PAC_UP = load_sprite_images(90)
PAC_LEFT = load_sprite_images(180)
PAC_RIGHT = load_sprite_images(270)

How do you update a scoreboard using tkinter

I am creating a brick breaker game using tkinter, but cannot figure out how to update the scoreboard. I do not get any error messages. The only problem is that the scoreboard stays at zero throughout the game. It should add 10 points each time a block is hit. Any suggestions would be amazing.
from tkinter import *
import random
import time
tk = Tk()
tk.title("Game")
tk.resizable(0, 0)
tk.wm_attributes("-topmost", 1)
canvas = Canvas(tk, width=500, height=400, bd=0, highlightthickness=0)
canvas.pack()
tk.update()
class Ball:
def __init__(self, canvas, paddle, score, block1, block2, block3, block4, block5, color):
self.canvas = canvas
self.paddle = paddle
self.block1 = block1
self.block2 = block2
self.block3 = block3
self.block4 = block4
self.block5 = block5
self.score = score
self.id = canvas.create_oval(10, 10, 25, 25, fill=color)
self.canvas.move(self.id, 245, 100)
starts = [-3, -2, -1, 1, 2, 3]
random.shuffle(starts)
self.x = starts[0]
self.y = -3
self.canvas_height = self.canvas.winfo_height()
self.canvas_width = self.canvas.winfo_width()
self.hit_bottom = False
def draw(self):
self.canvas.move(self.id, self.x, self.y)
pos = self.canvas.coords(self.id)
if pos[1] <= 0:
self.y = 3
if self.hit_paddle(pos) == True:
self.y = -3
if pos[3] >= self.canvas_height:
self.hit_bottom = True
if pos[0] <= 0:
self.x = 3
if pos[2] >= self.canvas_width:
self.x = -3
if self.hit_block(pos) == True:
self.y = +3
if self.hit_block == True:
self.scoreboard = self.score_board + self.add_score
def hit_paddle(self, pos):
paddle_pos = self.canvas.coords(self.paddle.id)
if pos[2] >= paddle_pos[0] and pos[0] <= paddle_pos[2]:
if pos[3] >= paddle_pos[1] and pos[3] <= paddle_pos[3]:
return True
return False
def hit_block(self,pos):
block1_pos = self.canvas.coords(self.block1.id)
if block1_pos and (pos[2] >= block1_pos[0] and pos[0] <= block1_pos[2]):
if block1_pos and(pos[3] >= block1_pos[1] and pos[3] <= block1_pos[3]):
self.canvas.delete(self.block1.id)
return True
block2_pos = self.canvas.coords(self.block2.id)
if block2_pos and (pos[2] >= block2_pos[0] and pos[0] <= block2_pos[2]):
if block2_pos and(pos[3] >= block2_pos[1] and pos[3] <= block2_pos[3]):
self.canvas.delete(self.block2.id)
return True
block3_pos = self.canvas.coords(self.block3.id)
if block3_pos and (pos[2] >= block3_pos[0] and pos[0] <= block3_pos[2]):
if block3_pos and(pos[3] >= block3_pos[1] and pos[3] <= block3_pos[3]):
self.canvas.delete(self.block3.id)
return True
block4_pos = self.canvas.coords(self.block4.id)
if block4_pos and (pos[2] >= block4_pos[0] and pos[0] <= block4_pos[2]):
if block4_pos and(pos[3] >= block4_pos[1] and pos[3] <= block4_pos[3]):
self.canvas.delete(self.block4.id)
return True
block5_pos = self.canvas.coords(self.block5.id)
if block5_pos and (pos[2] >= block5_pos[0] and pos[0] <= block5_pos[2]):
if block5_pos and(pos[3] >= block5_pos[1] and pos[3] <= block5_pos[3]):
self.canvas.delete(self.block5.id)
return True
return False
class Score:
def __init__(self, canvas):
self.canvas = canvas
self.add_score = 10
self.scoreboard = 0
self.id = canvas.create_text(50,25, text='Your score = %s' %(self.scoreboard))
class Paddle:
def __init__(self, canvas, color):
self.canvas = canvas
self.id = canvas.create_rectangle(0, 0, 100, 10, fill=color)
self.canvas.move(self.id, 200, 300)
self.x = 0
self.canvas_width = self.canvas.winfo_width()
self.canvas.bind_all('<KeyPress-Left>', self.turn_left)
self.canvas.bind_all('<KeyPress-Right>', self.turn_right)
def turn_left(self, evt):
self.x = -2
def turn_right(self, evt):
self.x = 2
def draw(self):
self.canvas.move(self.id, self.x, 0)
pos = self.canvas.coords(self.id)
if pos[0] <= 0:
self.x = 0
elif pos[2] >= self.canvas_width:
self.x = 0
class Block:
def __init__(self, canvas, x, y, color):
self.canvas = canvas
self.x = int(x)
self.y = int(y)
self.id = canvas.create_rectangle(self.x, self.y, self.x + 50, self.y + 10, fill=color)
self.canvas.move(self.id, self.x, self.y)
block1 = Block(canvas, 50, 25, 'orange')
block2 = Block(canvas, 90, 25, 'yellow')
block3 = Block(canvas, 130, 25, 'green')
block4 = Block(canvas, 170, 25, 'blue')
block5 = Block(canvas, 210, 25, 'violet')
score = Score(canvas)
paddle = Paddle(canvas, 'blue')
ball = Ball(canvas, paddle, score, block1, block2, block3, block4, block5, 'red')
while 1:
if ball.hit_bottom == False:
ball.draw()
paddle.draw()
tk.update_idletasks()
tk.update()
time.sleep(0.01)
After you change self.scoreboard you have to replace text manually
canvas.itemconfig(self.id, text='Your score = %s' % (self.scoreboard))
effbot.org: The Tkinter Canvas Widget
BTW: If you use IntVar() to keep score then you could use trace('w', callback) to assign function callback to this IntVar() which could change this text on canvas everytime you change value in this IntVar()
effbot.org: The Variable Classes (BooleanVar, DoubleVar, IntVar, StringVar)

Resources