Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/356.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在窗口的下部绘制PyQt5_Python_Pyqt5 - Fatal编程技术网

Python 在窗口的下部绘制PyQt5

Python 在窗口的下部绘制PyQt5,python,pyqt5,Python,Pyqt5,我试图制作一个只在屏幕的半按钮上绘制的程序,但是正如你所看到的,绘画向底部移动,如果我在上半部分绘制,绘画发生在下半部分。 我想直接在底部画 这是我的密码: from PIL.ImageEnhance import Color from PyQt5.QtWidgets import QMainWindow, QApplication, QMenu, QMenuBar, QAction, QFileDialog, QTextEdit, QVBoxLayout, \ QWidget, QL

我试图制作一个只在屏幕的半按钮上绘制的程序,但是正如你所看到的,绘画向底部移动,如果我在上半部分绘制,绘画发生在下半部分。 我想直接在底部画

这是我的密码:

from PIL.ImageEnhance import Color
from PyQt5.QtWidgets import QMainWindow, QApplication, QMenu, QMenuBar, QAction, QFileDialog, QTextEdit, QVBoxLayout, \
    QWidget, QLabel
from PyQt5.QtGui import QIcon, QImage, QPainter, QPen, QBrush, QPixmap
from PyQt5.QtCore import Qt, QPoint, QSize, QRect
import sys

import pyautogui



class Window(QMainWindow):
    def __init__(self):
        super().__init__()

        title = "Digital Waraq"

        icon = "icons/pain.png"
        [x, y] = pyautogui.size()
        self.setWindowTitle(title)
        self.setGeometry(0, 0, x, y)
        self.setWindowIcon(QIcon(icon))



        self.image = QImage(pyautogui.size().width, int(pyautogui.size().height/2), QImage.Format_RGB32)
        self.image.fill(Qt.gray)

        self.drawing = False
        self.brushSize = 2
        self.brushColor = Qt.black
        self.lastPoint = QPoint()



    def mousePressEvent(self, event):
        if event.button() == Qt.LeftButton:
            self.drawing = True
            self.lastPoint = event.pos()
            #print(self.lastPoint)


    def mouseMoveEvent(self, event):
        if(event.buttons() & Qt.LeftButton) & self.drawing:
            painter = QPainter(self.image)
            painter.setPen(QPen(self.brushColor, self.brushSize, Qt.SolidLine, Qt.RoundCap, Qt.RoundJoin))
            painter.drawLine(self.lastPoint, event.pos())
            self.lastPoint = event.pos()
            self.update()



    def mouseReleaseEvent(self, event):

        if event.button() == Qt.LeftButton:
            self.drawing = False


    def paintEvent(self, event):
        canvasPainter  = QPainter(self)
        #canvasPainter.drawImage(self.rect(), self.image, self.image.rect())
        newRect = QRect(QPoint(0, int(pyautogui.size().height/2)), QSize(self.image.size()))
        #canvasPainter.drawImage(newRect, self.image, self.image.rect())
        canvasPainter.drawImage(newRect, self.image, self.image.rect())


if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = Window()
    window.show()
    sys.exit(app.exec())

不要使在小部件中划分绘制区域的任务复杂化,一个更简单的解决方案是创建一个小部件,在该小部件中完成绘制,然后将其放置在主窗口的底部

导入系统 从PyQt5.QtCore导入QPoint,Qt 从PyQt5.QtGui导入QBrush、QGuiApplication、QImage、QPainter、QPen 从PyQt5.QtWidgets导入QApplication、QMainWindow、QVBoxLayout、QWidget 类抽屉(QWidget): def uuu init uuu(self,parent=None): super()。\uuuu init\uuuu(父级) 自绘制=错误 self.last_point=QPoint() self.\u image\u layer=QImage(self.size(),QImage.Format\u RGB32) 自身图像层填充(Qt.gray) self.brushSize=2 self.brushColor=Qt.black def鼠标压力事件(自身、事件): 自绘制=真 self.last_point=event.pos() def mouseMoveEvent(自身、事件): 如果self.\u绘图和event.buttons()&Qt.LeftButton: painter=QPainter(自身图像层) 画师:塞滕( QPen( 自我色彩, 自我尺寸, Qt.SolidLine, Qt.RoundCap, Qt.RoundJoin, ) ) 绘制线(self.last_点,event.pos()) self.last_point=event.pos() self.update() def mouseReleaseEvent(自身,事件): 自绘制=真 def paintEvent(自身,事件): 油漆工=油漆工(自身) painter.drawImage(QPoint(),self.\u image\u层) (完) def resizeEvent(自我,事件): 如果( self.size().width()>self.\u image\u layer.width() 或self.size().height()>self.\u image\u layer.height() ): qimg=QImage( 最大值(self.size().width(),self.\u image\u layer.width()), 最大值(self.size().height(),self.\u image\u layer.height()), QImage.Format_RGB32, ) 充气(Qt.灰色) 油漆工=油漆工(qimg) painter.drawImage(QPoint(),self.\u image\u层) (完) self.\u image\u layer=qimg self.update() 类窗口(QMainWindow): def uuu init uuu(self,parent=None): super()。\uuuu init\uuuu(父级) self.drawer=drawer() central_widget=QWidget() self.setCentralWidget(中心窗口小部件) vlay=QVBoxLayout(中心窗口小部件) vlay.setContentsMargins(0,0,0,0) vlay.addStretch(1) vlay.addWidget(self.drawer,stretch=1) r=QGuiApplication.primaryScreen().availableGeometry() 自置几何(r) 如果名称=“\uuuuu main\uuuuuuuu”: app=QApplication(sys.argv) window=window() window.show() sys.exit(app.exec())
感谢您的帮助,我对GUI python还不熟悉。但现在,当我运行应用程序时,什么都没有显示!我的代码运行正常,当我切换到你的代码时,什么都没有发生。谢谢你的帮助,我正在使用PyCharm运行代码,它应该可以工作,但什么都没有发生!