Im using PyQt5 and I know you can make other things like buttons move to other positions with move() but that isn't working for me in this instance, however it is working when I set up the positions for some of the buttons I made earlier.
For some reason when I try to make my widget DrawImage go to a certain part of the window It never does and instead just stays in the top left corner.
This DrawImage is going to be inserted into another class I have that sets up the window DrawImage will be placed in. Any ideas on how I can make this work?
import PyQt5
import sys
#from PyQt5 import QtCore, QtGui, QtWidgets
#from PyQt.QtWidgets import QApplication, QWidget
from PyQt5 import QtWidgets
from PyQt5 import QtGui, QtCore
class Homework4(QtWidgets.QMainWindow):
def __init__(self):
QtWidgets.QWidget.__init__(self)
self.setup()
def setup(self):
self.setGeometry(200, 200, 400, 200)
self.setWindowTitle('Triangle Peg Game')
self.setToolTip("Play the triangle peg game!")
self.Left_button = LeftBtn(self)
self.Right_button = RightBtn(self)
self.Top_Button = TopBtn(self)
self.Down_Button = DownBtn(self)
#setting the background color
p = self.palette()
p.setColor(self.backgroundRole(), QtCore.Qt.gray)
#p.setColor(self.backgroundRole(), Qt.gray)
self.setPalette(p)
self.setAutoFillBackground(True)
#Central Widget
self.Draw = DrawImage(self) #############
self.setCentralWidget(self.Draw)
self.show()
class LeftBtn(QtWidgets.QPushButton):
def __init__(self, parent):
QtWidgets.QPushButton.__init__(self, parent)
self.setText("Left")
self.resize(40,200)
self.move(0,0)
#self.clicked.connect(QtWidgets.qApp.quit) #bind the button to an event slot
class RightBtn(QtWidgets.QPushButton):
def __init__(self, parent):
QtWidgets.QPushButton.__init__(self, parent)
self.setText("Right")
self.resize(40,200)
self.move(360,0)
#self.clicked.connect(QtWidgets.qApp.quit)
class TopBtn(QtWidgets.QPushButton):
def __init__(self, parent):
QtWidgets.QPushButton.__init__(self, parent)
self.setText("Top")
self.resize(320,20)
self.move(40,0)
#self.clicked.connect(QtWidgets.qApp.quit)
class DownBtn(QtWidgets.QPushButton):
def __init__(self, parent):
QtWidgets.QPushButton.__init__(self, parent)
self.setText("Down")
self.resize(320,20)
self.move(40,180)
#self.clicked.connect(QtWidgets.qApp.quit)
This is my DrawImage class:
class DrawImage(QtWidgets.QWidget):
def __init__(self, parent):
QtWidgets.QWidget.__init__(self, parent)
self.move(200,50)
self.setFizedSize(100,100)
def paintEvent(self, event): #use to draw on the canvas
paint = QtGui.QPainter()
paint.begin(self)
paint.setRenderHint(QtGui.QPainter.Antialiasing)
# make a white drawing background
paint.setBrush(QtCore.Qt.white)
paint.drawRect(event.rect())
# for circle make the ellipse radii match
radx = 10
rady = 10
# draw red circles
paint.setPen(QtCore.Qt.red)
for k in range(12, 22, 1): # 125, 220, 10
center = QtCore.QPoint(k, k)
# optionally fill each circle yellow
paint.setBrush(QtCore.Qt.yellow)
paint.drawEllipse(center, radx, rady)
paint.end()
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
main_window = Homework4() #create an object of Homework4, which contains the main window
app.exec_()