插入在另一个类中创建的Pixmap

时间:2016-04-19 13:20:51

标签: python pyqt qpixmap

我正在尝试创建QMainWindow并添加一些QDockWidgetQPushButtons并在其中添加图片。我在不同的类中创建了图像,以便稍后添加一些特征。

我需要将按钮和图像放在QMainWindow的左侧。像这样:

enter image description here

正如您所看到的,那里有一些QPushButtonQDockWidget,每个都有一个matplotlib数字。

当我想添加图像时问题就出现了。我需要它位于QMainWindow的左下角。

这是代码的一部分:

class MainWindow(QMainWindow):
  def __init__(self):
    super(MainWindow, self).__init__()

    self.my_widget = QWidget()
    self.setCentralWidget(self.my_widget)

    self.createDockWindows()
    self.createButtons()

  def createDockWindows(self):
    #Here I create two QDockWidget and
    #I add into them a matplotlib figure created in another class

    self.fig1 = mpl_Canvas()    #This is the matplotlib figure`s class
    self.fig2 = mpl_Canvas()

    dock1.setWidget(self.fig1)
    dock2.setWidget(self.fig2)

    self.addDockWidget(Qt.RightDockWidgetArea, dock1)
    self.addDockWidget(Qt.RightDockWidgetArea, dock2)

def createButtons(self):
    #Here i add some buttons  
    layout = QVBoxLayout()

    self.r_btn = QRadioButton()
    self.r_btn.setGeometry(10, 50, 10, 30)
    self.r_btn.setText("RadioButton 1")

    ###HERE IS WHERE I NEED TO ADD THE IMAGE FROM THE CREATEIMAGE CLASS###

    self.image = createImage()

    layout.addWidget(self.r_btn)
    layout.addWidget(self.image)

    self.my_widget.setLayout(layout)

class createImage(QWidget):
  def __init__(self):
    QWidget.__init__(self)

    self.showImage()

  def showImage(self):
    self.label = QLabel()
    self.img = QPixmap("image.jpg")
    self.img_scaled = self.img.scaled(self.label.size(),Qt.KeepAspectRatio)

    self.label.setPixmap(self.img_scaled)

我该怎么做?我知道我做错了但我找不到错误。

希望你能帮助我。

--------编辑---------

这是来自@ K.Mulier的修改代码:

class MainWindow(QMainWindow):
  def __init__(self):
    super(MainWindow, self).__init__()

    self.my_widget = QWidget()
    self.setCentralWidget(self.my_widget)

    self.createDockWindows()
    self.createButtons()

  def createDockWindows(self):
    dock1 = QDockWidget("Osciloscope",self)
    dock1.setAllowedAreas(Qt.LeftDockWidgetArea | Qt.RightDockWidgetArea)

    dock2 = QDockWidget("Espectrometer", self)
    dock2.setFeatures(QDockWidget.DockWidgetFloatable | QDockWidget.DockWidgetMovable
        | QDockWidget.DockWidgetClosable)

    dock1.setMinimumSize(800,100)
    dock2.setMinimumSize(800,100)

    self.fig1 = mpl_Canvas()
    self.fig2 = mpl_Canvas()

    dock1.setWidget(self.fig1)
    dock2.setWidget(self.fig2)

    self.addDockWidget(Qt.RightDockWidgetArea, dock1)
    self.addDockWidget(Qt.RightDockWidgetArea, dock2)

  def createButtons(self):
    layout = QVBoxLayout()

    self.r_btn = QRadioButton()
    self.r_btn.setGeometry(10, 50, 10, 30)
    self.r_btn.setText("RadioButton 1")

    self.image = createImage()

    layout.addWidget(self.r_btn)
    layout.addWidget(self.image)

    self.my_widget.setLayout(layout)

class createImage(QWidget):
  def __init__(self):
    super(createImage, self).__init__(parent = None)
    self.mainLayout = QHBoxLayout()
    self.setLayout(self.mainLayout)

    self.showImage()


  def showImage(self):

    picPath = os.getcwd() + "C:\Users\Flosh\Desktop\hello.png"
    print picPath
    picMap = QPixmap(picPath)

    picLabel = QLabel(parent = None)
    picLabel.setGeometry(10,10,800,600)
    picLabel.setPixmap(picMap.scaled(780,580,Qt.KeepAspectRatio))
    self.mainLayout.addWidget(picLabel)

class mpl_canvas(self):
  #A matplotlib figure

编辑 - 问题已解决

上一个代码中的问题在于以下几行:

picPath = os.getcwd() + "C:\somedirectory\image.png"
print picPath
picMap = QPixmap(picPath) 

您必须将picPath中的picMap = QPixmap(picPath)替换为图片的位置,如下所示:

picMap = QPixmap(C:\somedirectory\image.png")

1 个答案:

答案 0 :(得分:1)

试试这个:

class MyImage(QWidget):

    def __init__(self):
        super(MyImage, self).__init__(parent = None)
        self.mainLayout = QtGui.QHBoxLayout()
        self.setLayout(self.mainLayout)
        self.setPicture()

    def setPicture(self):
        picPath = os.getcwd() + "/someFolder/image.jpg"
        print(picPath) # Check if the path to your picture is correct.
        picMap = QtGui.QPixmap(picPath)

        picLabel = QtGui.QLabel(parent = None)
        picLabel.setGeometry(10,10,800,600)
        picLabel.setPixmap(picMap.scaled(780,580,QtCore.Qt.KeepAspectRatio))
        self.mainLayout.addWidget(picLabel)

如果您还有其他问题,请不要犹豫。我很乐意帮助你。

修改

我已经对您的代码进行了全面测试。 显然它不适用于.jpg文件,只适用于.png文件。所以你应该将你的图片转换为.png文件,然后运行这段代码(这是你的代码,一些细微的变化):

import os
import sys

from PyQt4.QtGui import QMainWindow
from PyQt4.QtGui import QWidget
from PyQt4.QtGui import QLabel
from PyQt4.QtGui import QDockWidget
from PyQt4.QtGui import QVBoxLayout
from PyQt4.QtGui import QRadioButton
from PyQt4.QtGui import QPixmap
from PyQt4 import QtGui
from PyQt4 import QtCore

class MainWindow(QMainWindow):
    def __init__(self):
        super(MainWindow, self).__init__()

        self.my_widget = QWidget()
        self.setCentralWidget(self.my_widget)

        self.createDockWindows()
        self.createButtons()

        self.show()

    def createDockWindows(self):
        #Here I create two QDockWidget and
        #I add into them a matplotlib figure created in another class

        # I do not have your matplotlib figures, so I've inserted
        # some labels instead.
        self.fig1 = QLabel("Hello")    #This is the matplotlib figure`s class
        self.fig2 = QLabel("World")

        dock1 = QDockWidget()
        dock2 = QDockWidget()

        dock1.setWidget(self.fig1)
        dock2.setWidget(self.fig2)

        self.addDockWidget(0x2, dock1)
        self.addDockWidget(0x2, dock2)

    def createButtons(self):
        #Here i add some buttons  
        layout = QVBoxLayout()

        self.r_btn = QRadioButton()
        self.r_btn.setGeometry(10, 50, 10, 30)
        self.r_btn.setText("RadioButton 1")

        ###HERE IS WHERE I NEED TO ADD THE IMAGE FROM THE CREATEIMAGE CLASS###

        self.image = MyImage()

        layout.addWidget(self.r_btn)
        layout.addWidget(self.image)

        self.my_widget.setLayout(layout)

class MyImage(QWidget):

    def __init__(self):
        super(MyImage, self).__init__(parent = None)
        self.mainLayout = QtGui.QHBoxLayout()
        self.setLayout(self.mainLayout)
        self.setPicture()

    def setPicture(self):
        # For some reason, it only works on .png files, not on .jpg files!
        picPath = os.getcwd() + "\someFolder\myImage.png"
        print(picPath) # Check if the path to your picture is correct.
        picMap = QtGui.QPixmap(picPath)

        picLabel = QtGui.QLabel(parent = None)
        picLabel.setGeometry(10,10,800,600)
        picLabel.setPixmap(picMap.scaled(780,580,QtCore.Qt.KeepAspectRatio))
        self.mainLayout.addWidget(picLabel)


if __name__== '__main__':
    app = QtGui.QApplication(sys.argv)
    QtGui.QApplication.setStyle(QtGui.QStyleFactory.create('Plastique'))
    myGUI = MainWindow()

    sys.exit(app.exec_())

请运行此代码,并告诉我它是否适合您。

第二次编辑

以下是一些额外的评论,以使其发挥作用:

(1).png文件应该是真正的' .png文件。只需将.jpg文件重命名为.png文件即可。我试过了。

(2)请不要忘记os.getcwd()已经暗示了很大一部分路径。因此,检查print语句的输出非常重要:print(picPath)。检查此路径是否正确引用您的图片。

第三次编辑

即使使用Paint将.jpg图片转换为.png图片也行不通。如果仍然出现错误,请访问此网站下载正版.png格式图片。并尝试使用那个: http://www.flaticon.com/free-icon/mortarboard_123402