0

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_()
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
ParPro
  • 197
  • 1
  • 6
  • 16

2 Answers2

0

QMainWindow has a preset layout as shown below:

enter image description here

this layout is used to set the toolbar, menubar, statusbar, dockwidgets and the centralwidget. The layouts handle the position and the size of the widgets, that is why this error is generated. The solution is to set another widget as a central widget, and your DrawImage is the son of this new widget.

class Homework4(QtWidgets.QMainWindow):
    def __init__(self):
        QtWidgets.QMainWindow.__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
        central_widget = QtWidgets.QWidget()
        self.Draw = DrawImage(central_widget)
        self.setCentralWidget(central_widget)

        self.show()
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
0

The QMainWindow class you are using has a special concept of a "central" widget. It sounds like you'd rather make your main widget not the central widget, but a "Dock widget". You need to familiarize yourself with QMainWindow and the layouts QWidgets provide.

user9170
  • 950
  • 9
  • 18