我正在使用PyQt5,我知道您可以使用move()将按钮移至其他位置,但是在这种情况下这对我不起作用,但是当我为某些位置设置位置时,它就起作用了我之前做的按钮。
由于某种原因,当我尝试使小部件DrawImage进入窗口的特定部分时,它从不执行操作,而是停留在左上角。
此DrawImage将插入另一个我将设置DrawImage窗口的类中。关于如何进行此工作的任何想法?
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)
这是我的DrawImage类:
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_()
答案 0 :(得分:0)
QMainWindow
具有如下所示的预设布局:
此布局用于设置toolbar
,menubar
,statusbar
,dockwidget
和centralwidget
。布局处理小部件的位置和大小,这就是生成此错误的原因。解决方案是将另一个窗口小部件设置为中央窗口小部件,而您的DrawImage
是这个新窗口小部件的儿子。
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()
答案 1 :(得分:0)
您使用的QMainWindow类具有“中央”窗口小部件的特殊概念。听起来您想让主窗口小部件不是中央窗口小部件,而是“停靠窗口小部件”。您需要熟悉QMainWindow和QWidgets提供的布局。