Пишу небольшой мессенджер, не могу понять, в чём причина отсутствия виджета.
Питоновский код:
import sys
from datetime import datetime
from PyQt5 import uic
from PyQt5 import QtWidgets
class Message(QtWidgets.QWidget):
def __init__(self, name, text):
super().__init__()
self.data = datetime.now().strftime('%H:%M' + ', ' + name + ':') # получение строки с данными о сообщении
# установка виджетов
self.info = QtWidgets.QLabel(self.data)
self.info.resize(20, 100)
self.info.move(10, 5)
self.info = QtWidgets.QLabel(text)
self.info.resize(20, 100)
self.info.move(35, 5)
class MyWidget(QtWidgets.QMainWindow):
def __init__(self):
super().__init__()
uic.loadUi('untitled.ui', self)
self.send_btn.clicked.connect(self.run)
def run(self):
item = QtWidgets.QListWidgetItem()
custom_item = Message('Name', self.mes_input.text())
self.messages_panel.addItem(item)
self.messages_panel.setItemWidget(item, custom_item)
self.mes_input.setText('')
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
ex = MyWidget()
ex.show()
sys.exit(app.exec_())
Код ui:
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>730</width>
<height>516</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralwidget">
<widget class="QPushButton" name="send_btn">
<property name="geometry">
<rect>
<x>630</x>
<y>420</y>
<width>91</width>
<height>41</height>
</rect>
</property>
<property name="text">
<string>PushButton</string>
</property>
</widget>
<widget class="QLineEdit" name="mes_input">
<property name="geometry">
<rect>
<x>20</x>
<y>420</y>
<width>601</width>
<height>31</height>
</rect>
</property>
</widget>
<widget class="QListWidget" name="messages_panel">
<property name="geometry">
<rect>
<x>20</x>
<y>10</y>
<width>701</width>
<height>401</height>
</rect>
</property>
</widget>
</widget>
<widget class="QMenuBar" name="menubar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>730</width>
<height>26</height>
</rect>
</property>
</widget>
<widget class="QStatusBar" name="statusbar"/>
</widget>
<resources/>
<connections/>
</ui>