首页 > 解决方案 > PyQt5:为什么我无法显示像素图?

问题描述

我正在尝试pixmap使用以下代码在我的应用程序中显示 a:

def add_controls(self):
    wid = QWidget(self)
    self.setCentralWidget(wid)

    pixmap = QPixmap("python.jpg")
    self.label = QLabel(self)
    self.label.setPixmap(pixmap)

    control_area = QVBoxLayout()  # centralWidget

    control_area.addWidget(self.label)

    wid.setLayout(control_area)

运行此代码时看不到图像,并且不存在错误

我也尝试过使用图像的完整路径,但这仍然无济于事。我正在从 Pycharm 2019.1.1 启动脚本。

我在这里做错了吗?任何建议,将不胜感激


“完整”代码,以防万一。

def setup_UI(self):
    # Add file menu
    menubar = self.menuBar()
    filemenu = menubar.addMenu('File')
    windowmenu = menubar.addMenu('Window')

    help_act = QAction('Open Help', self)
    folder_act = QAction('Open Folder', self)
    theme_act = QAction('Toggle light/dark theme', self)

    folder_act.setShortcut('Ctrl+D')
    help_act.setShortcut('Ctrl+H')
    theme_act.setShortcut('Ctrl+T')

    filemenu.addAction(folder_act)
    filemenu.addAction(help_act)
    windowmenu.addAction(theme_act)

    help_act.triggered.connect(self.open_help)
    folder_act.triggered.connect(self.add_files)
    theme_act.triggered.connect(self.toggle_colors)

    self.createTable()
    self.add_controls()

    self.setWindowTitle(self.title)
    self.setGeometry(self.left, self.top, self.width, self.height)
    self.toggle_colors()
    self.show()

def add_controls(self):
    wid = QWidget(self)
    self.setCentralWidget(wid)

    # Add song controls
    volumeslider = QSlider(Qt.Horizontal, self)
    volumeslider.setFocusPolicy(Qt.NoFocus)
    volumeslider.valueChanged[int].connect(self.change_volume)
    volumeslider.setValue(100)

    play_btn = QPushButton('Play')  # play button
    pause_btn = QPushButton('Pause')  # pause button
    stop_btn = QPushButton('Stop')  # stop button

    # Add playlist controls
    prev_btn = QPushButton('Prev')
    shuffle_btn = QPushButton('Shuffle')
    next_btn = QPushButton('Next')
    sort_btn = QPushButton('Sort')
    search_btn = QPushButton('Search')
    remove_btn = QPushButton('Remove')

    pixmap = QPixmap("python.jpg")
    self.label = QLabel(self)
    self.label.setPixmap(pixmap)

    # Add button layouts
    control_area = QVBoxLayout()  # centralWidget
    controls = QHBoxLayout()
    playlist_ctrl_layout = QHBoxLayout()
    playlist_func = QHBoxLayout()

    self.cb1 = QComboBox()
    self.cb1.addItem("Sort by Title")
    self.cb1.addItem("Sort by Album")
    self.cb1.addItem("Sort by Artist")
    self.cb1.currentIndexChanged.connect(self.selectionchange)

    self.cb2 = QComboBox()
    self.cb2.addItem("Search by Title")
    self.cb2.addItem("Search by Album")
    self.cb2.addItem("Search by Artist")
    self.cb2.currentIndexChanged.connect(self.selectionchange)

    # Add buttons to song controls layout
    controls.addWidget(play_btn)
    controls.addWidget(pause_btn)
    controls.addWidget(stop_btn)

    # Add buttons to playlist controls layout
    playlist_ctrl_layout.addWidget(prev_btn)
    playlist_ctrl_layout.addWidget(shuffle_btn)
    playlist_ctrl_layout.addWidget(next_btn)

    playlist_func.addWidget(search_btn)
    playlist_func.addWidget(remove_btn)

    # Add to vertical layout
    control_area.addLayout(controls)
    control_area.addLayout(playlist_ctrl_layout)
    control_area.addLayout(playlist_func)
    control_area.addWidget(volumeslider)
    control_area.addWidget(self.tableWidget)
    control_area.addWidget(self.cb1)
    control_area.addWidget(self.label)

    wid.setLayout(control_area)

    # Connect each signal to their appropriate function
    play_btn.clicked.connect(self.play_handler)
    pause_btn.clicked.connect(self.pause_handler)
    stop_btn.clicked.connect(self.stop_handler)

    prev_btn.clicked.connect(self.prev_song)
    shuffle_btn.clicked.connect(self.shuffle_list)
    next_btn.clicked.connect(self.next_song)

    sort_btn.clicked.connect(self.sort_handler)
    search_btn.clicked.connect(self.search_handler)
    remove_btn.clicked.connect(self.remove_handler)

    self.statusBar()

标签: python-3.xpyqt5

解决方案


您输入了无效的图像名称或扩展名,或者图像位于不同的目录中.. 检查拼写python.jpg

import sys
from PyQt5.QtCore    import *
from PyQt5.QtGui     import *
from PyQt5.QtWidgets import * 

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

        self.add_controls()

    def add_controls(self):
        wid = QWidget(self)
        self.setCentralWidget(wid)

        pixmap = QPixmap("im.png").scaled(250, 250)         # im.png
        self.label = QLabel(self, alignment=Qt.AlignCenter)
        self.label.setPixmap(pixmap)

        control_area = QVBoxLayout()      # centralWidget
        control_area.addWidget(self.label)

        wid.setLayout(control_area)


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

在此处输入图像描述


推荐阅读