Dobrý den, já čet jednu knihu na zetcode.com a pak jsem spadnul do takéto chyby: https://imagebin.ca/v/40NubbcB2IR0
Muuj kód byl:
#!/usr/bin/python3
# -*- coding: utf-8 -*-
"""
ZetCode PyQt5 tutorial
In the example, we draw randomly 1000 red points
on the window.
Author: Jan Bodnar, changed by lesna.vevericka@gmail.com
Website: zetcode.com, artesiumtech.wordpress.com, autoskola-free.szm.com
Last edited: August 2017, may 2018
"""
from PyQt5.QtWidgets import QWidget, QApplication
from PyQt5.QtGui import QPainter, QPen
from PyQt5.QtCore import Qt,QPoint
import sys, random
class Example(QWidget):
def __init__(self):
super().__init__()
self.initUI()
self.setMouseTracking(True)
def initUI(self):
self.setGeometry(0, 0, 1000, 1000)
self.setWindowTitle('Anti Stresák - klikaj')
self.show()
def paintEvent(self, e):
qp = QPainter()
qp.begin(self)
self.drawPoints(qp)
qp.end()
def drawPoints(self,qp):
colors = [Qt.black,Qt.red,Qt.darkRed,Qt.green,Qt.darkGreen,Qt.blue,Qt.darkBlue,Qt.cyan,Qt.darkCyan,Qt.magenta,Qt.darkMagenta,Qt.yellow,Qt.darkYellow,Qt.gray,Qt.darkGray,Qt.lightGray]
qp.setPen(QPen(colors[random.randint(0,len(colors)-1)],random.randint(40,150),Qt.SolidLine))
size = self.size()
x = random.randint(1, size.width()-1)
y = random.randint(1, size.height()-1)
if self.pos is QPoint:
print("isQ")#tu som chcel odchytit suradnice pre klik a drawPoint(x,y), ale NEIDE :-(
qp.drawPoint(QPoint.x(self.pos),QPoint.y(self.pos))#toto bola moja logika, ale EE...
qp.drawPoint(x,y)
print(self.pos)
#def mouseMoveEvent(self, event):
def mousePressEvent(self,event):
self.pos = event.pos()
self.update()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())