1

Файл Main.py:

import sys
from Widget import Widget
from PyQt5.QtWidgets import QApplication

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

Файл Widget.py

from PyQt5.QtWidgets import QWidget, QLabel
from PyQt5 import uic


class Widget(QWidget):
    def __init__(self):
        super().__init__()
        uic.loadUi("D:\\users\\vasilev_av\\Documents\\Workspace\\QtWorkspace\\untitled2\\widget.ui", self)

Пробую так:

self.label.setPixmap(pixmap)

Но получаю ошибку :

self.label.setPixmap(pixmap)

AttributeError: 'Widget' object has no attribute 'label'

1 ответ 1

4

Такое может произойти если вы поменяли OjbectName у вашего QLabel. Посмотрите в дизайнере ObjectName, или в файле widget.ui строку:

<widget class="QLabel" name="label">

Вот пример с вашим кодом:

Файл test_ui.py

import sys
from PyQt5.QtWidgets import QApplication
from PyQt5.QtWidgets import QWidget, QLabel
from PyQt5 import uic

class Widget(QWidget):

    def __init__(self):
        super().__init__()
        uic.loadUi("test.ui", self)
        self.label.setText("NewText")

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

И файл формы test.ui:

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>Form</class>
 <widget class="QWidget" name="Form">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>400</width>
    <height>300</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>Form</string>
  </property>
  <widget class="QLabel" name="label">
   <property name="geometry">
    <rect>
     <x>130</x>
     <y>70</y>
     <width>59</width>
     <height>14</height>
     </rect>
   </property>
   <property name="text">
    <string>TextLabel</string>
   </property>
  </widget>
 </widget>
 <resources/>
 <connections/>
</ui>

Ваш ответ

Нажимая на кнопку «Отправить ответ», вы соглашаетесь с нашими пользовательским соглашением, политикой конфиденциальности и политикой о куки

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.