2017-02-24 93 views
0

我試圖改變我的Qpainter的大小,我無法弄清楚怎麼可能有人在這裏幫助我的代碼,我看了網上,我不能弄明白,因爲我需要的代碼是嵌入感謝您的幫助,不需要其他代碼。蟒蛇調整Qpainter

import sys 
import os 
from PyQt4.QtCore import QSize, QTimer 
from PyQt4.QtGui import QApplication,QGraphicsRectItem , QMainWindow, QPushButton, QWidget, QIcon, QLabel, QPainter, QPixmap, QMessageBox, QAction, QKeySequence, QFont, QFontMetrics, QMovie 
from PyQt4 import QtGui 


class UIWindow(QWidget): 
    def __init__(self, parent=None): 
     super(UIWindow, self).__init__(parent) 
     self.resize(QSize(400, 450)) 

    def paintEvent(self, event): 
     painter = QPainter(self) 
     painter.drawPixmap(self.rect(), QPixmap("Images\Image.png")) 
     painter.move(0,0) 
     painter.resize(950,270) 




class MainWindow(QMainWindow,): 
    def __init__(self, parent=None): 
     super(MainWindow, self).__init__(parent) 
     self.setGeometry(490, 200, 950, 620) 
     self.setFixedSize(950, 620) 
     self.startUIWindow() 
     self.setWindowIcon(QtGui.QIcon('Images\Logo.png')) 

    def startUIWindow(self): 
     self.Window = UIWindow(self) 
     self.setWindowTitle("pythonw") 
     self.setCentralWidget(self.Window) 
     self.show() 

if __name__ == '__main__': 
    app = QApplication(sys.argv) 
    w = MainWindow() 
    sys.exit(app.exec_()) 
+0

'QPainter'沒有'move()'方法,你想要做什麼? – eyllanesc

+0

QPainter是負責繪製小部件的類,它不是小部件或畫筆。 – eyllanesc

+0

@eyllanesc我試圖讓我的圖像出現在程序不同的頂部,而不是在中心和全部扭曲 – Tyrell

回答

2

的drawPixmap功能需要作爲第一個參數在那裏要繪製的矩形,即(0,0,寬度,高度)

def paintEvent(self, event): 
    painter = QPainter(self) 
    pixmap = QPixmap("Images\Image.png") 
    painter.drawPixmap(QRect(0, 0, pixmap.width(), pixmap.height()), pixmap) 

完整代碼:

import sys 
import os 
from PyQt4.QtCore import QSize, QTimer, QRect 
from PyQt4.QtGui import QApplication,QGraphicsRectItem , QMainWindow, QPushButton, QWidget, QIcon, QLabel, QPainter, QPixmap, QMessageBox, QAction, QKeySequence, QFont, QFontMetrics, QMovie 
from PyQt4 import QtGui 


class UIWindow(QWidget): 
    def __init__(self, parent=None): 
     super(UIWindow, self).__init__(parent) 
     self.resize(QSize(400, 450)) 

    def paintEvent(self, event): 
     painter = QPainter(self) 
     pixmap = QPixmap("Images\Image.png") 
     painter.drawPixmap(QRect(0, 0, pixmap.width(), pixmap.height()), pixmap) 


class MainWindow(QMainWindow,): 
    def __init__(self, parent=None): 
     super(MainWindow, self).__init__(parent) 
     self.setGeometry(490, 200, 950, 620) 
     self.setFixedSize(950, 620) 
     self.startUIWindow() 

    def startUIWindow(self): 
     self.Window = UIWindow(self) 
     self.setWindowTitle("pythonw") 
     self.setCentralWidget(self.Window) 
     self.show() 

if __name__ == '__main__': 
    app = QApplication(sys.argv) 
    w = MainWindow() 
    sys.exit(app.exec_()) 
+0

真棒謝謝你這麼多 – Tyrell