Как мне привязать QScrollArea к QLabel, чтобы все работало?
у меня не выходит вставить QLabel в QScrollArea.
Форму делаю в Qt Dtsigner.
main.py:
from PyQt5.QtWidgets import QApplication, QMainWindow
from PyQt5.uic import loadUi
class DerNeXApp(QMainWindow):
def __init__(self):
super(DerNeXApp, self).__init__()
loadUi('gui.ui', self)
self.pushButton.clicked.connect(self.on_button_click)
self.conversation_history = ""
self.setFixedSize(self.width(), self.height())
self.label.setWordWrap(True)
def on_button_click(self):
self.conversation_history += f"Вы: {user_input}.\nDerNeX: {response_text}.\n\n"
self.label.setText(self.conversation_history)
if __name__ == '__main__':
app = QApplication(sys.argv)
main_win = DerNeXApp()
main_win.show()
sys.exit(app.exec_())
gui.ui:
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>DerNeX</class>
<widget class="QMainWindow" name="DerNeX">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>640</width>
<height>700</height>
</rect>
</property>
<property name="windowTitle">
<string>DerNeX</string>
</property>
<property name="styleSheet">
<string notr="true">background-color: rgb(0, 0, 0);</string>
</property>
<widget class="QWidget" name="centralwidget">
<widget class="QTextEdit" name="textEdit">
<property name="geometry">
<rect>
<x>0</x>
<y>640</y>
<width>581</width>
<height>61</height>
</rect>
</property>
<property name="font">
<font>
<family>Accuratist.otf</family>
<pointsize>16</pointsize>
</font>
</property>
<property name="cursor" stdset="0">
<cursorShape>IBeamCursor</cursorShape>
</property>
<property name="contextMenuPolicy">
<enum>Qt::DefaultContextMenu</enum>
</property>
<property name="toolTip">
<string>input</string>
</property>
<property name="whatsThis">
<string><html><head/><body><p>Enter your text</p></body></html></string>
</property>
<property name="accessibleDescription">
<string notr="true"/>
</property>
<property name="styleSheet">
<string notr="true">background-color: rgb(20, 20, 20);
border-color: rgb(255, 255, 255);
color: rgb(255, 255, 255);</string>
</property>
<property name="html">
<string><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN" "http://www.w3.org/TR/REC-html40/strict.dtd">
<html><head><meta name="qrichtext" content="1" /><style type="text/css">
p, li { white-space: pre-wrap; }
</style></head><body style=" font-family:'Accuratist.otf'; font-size:16pt; font-weight:400; font-style:normal;">
<p style="-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; font-family:'MS Shell Dlg 2'; font-size:10pt;"><br /></p></body></html></string>
</property>
<property name="placeholderText">
<string/>
</property>
</widget>
<widget class="QPushButton" name="pushButton">
<property name="geometry">
<rect>
<x>580</x>
<y>640</y>
<width>61</width>
<height>61</height>
</rect>
</property>
<property name="font">
<font>
<pointsize>10</pointsize>
</font>
</property>
<property name="cursor">
<cursorShape>PointingHandCursor</cursorShape>
</property>
<property name="toolTip">
<string>send</string>
</property>
<property name="whatsThis">
<string><html><head/><body><p>Send</p></body></html></string>
</property>
<property name="styleSheet">
<string notr="true">background-color: rgb(42, 42, 42);
image: url(:/gifs/icons8-up-50.png);
border-color: rgb(255, 255, 255);
color: rgb(255, 255, 255);</string>
</property>
<property name="text">
<string/>
</property>
</widget>
<widget class="QLabel" name="label">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>621</width>
<height>641</height>
</rect>
</property>
<property name="font">
<font>
<family>Accuratist.otf</family>
<pointsize>15</pointsize>
</font>
</property>
<property name="styleSheet">
<string notr="true">color: rgb(127, 127, 127);
image: url(:/gifs/965233-200(1).png);
</string>
</property>
<property name="text">
<string/>
</property>
<property name="alignment">
<set>Qt::AlignLeading|Qt::AlignLeft|Qt::AlignTop</set>
</property>
<property name="margin">
<number>10</number>
</property>
</widget>
<widget class="QScrollBar" name="verticalScrollBar">
<property name="geometry">
<rect>
<x>620</x>
<y>0</y>
<width>20</width>
<height>641</height>
</rect>
</property>
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
</widget>
</widget>
</widget>
<resources>
<include location="send.qrc"/>
</resources>
<connections/>
</ui>
Ответы (1 шт):
Автор решения: S. Nick
→ Ссылка
То что вы хотите сделать в форме, может выглядеть примерно так.
Обратите внимание:
- я полностью переписал в Qt Designer модуль.ui;
- стили я добавляю в
app.setStyleSheet(Stylesheet); border-image: url(foto.jpg);- это ваша фоновая картинка.
main.py:
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow
from PyQt5.uic import loadUi
class DerNeXApp(QMainWindow):
def __init__(self):
super(DerNeXApp, self).__init__()
loadUi('q1560582_gui222.ui', self)
self.pushButton.clicked.connect(self.on_button_click)
self.conversation_history = ""
# ? self.setFixedSize(self.width(), self.height())
self.label.setWordWrap(True)
self.i = 1 # + для тестирования
def on_button_click(self):
# ----> vvvvvvvvvv <----------------------------------------------------------- ???
user_input = self.textEdit.toPlainText() # +
response_text = '12345 ' * self.i # +
# ----> ^^^^^^^^^^^^^ <-------------------------------------------------------- ???
self.conversation_history += f"Вы: {user_input}.\nDerNeX: {response_text}.\n\n"
self.label.setText(self.conversation_history)
self.i += 1 # + для тестирования
Stylesheet = """
#label {
color: rgb(255, 255, 255);
background-color: transparent; /* <---------------------------- */
}
#scrollArea {
border-image: url(foto.jpg); /* <---------------------------- */
}
"""
if __name__ == '__main__':
app = QApplication(sys.argv)
app.setStyleSheet(Stylesheet) # +++
main_win = DerNeXApp()
main_win.show()
sys.exit(app.exec_())
q1560582_gui222.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>469</width>
<height>458</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralwidget">
<layout class="QVBoxLayout" name="verticalLayout_2">
<item>
<layout class="QVBoxLayout" name="verticalLayout">
<property name="spacing">
<number>0</number>
</property>
<property name="bottomMargin">
<number>10</number>
</property>
<item>
<widget class="QScrollArea" name="scrollArea">
<property name="widgetResizable">
<bool>true</bool>
</property>
<widget class="QWidget" name="scrollAreaWidgetContents">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>447</width>
<height>316</height>
</rect>
</property>
<property name="styleSheet">
<string notr="true">background-color: transparent;</string>
</property>
<layout class="QHBoxLayout" name="horizontalLayout_3">
<item>
<widget class="QLabel" name="label">
<property name="font">
<font>
<family>Calibri</family>
<pointsize>16</pointsize>
<italic>false</italic>
</font>
</property>
<property name="text">
<string/>
</property>
</widget>
</item>
</layout>
</widget>
</widget>
</item>
<item>
<widget class="QFrame" name="frame">
<property name="maximumSize">
<size>
<width>16777215</width>
<height>111</height>
</size>
</property>
<property name="frameShape">
<enum>QFrame::StyledPanel</enum>
</property>
<property name="frameShadow">
<enum>QFrame::Raised</enum>
</property>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QTextEdit" name="textEdit">
<property name="minimumSize">
<size>
<width>0</width>
<height>100</height>
</size>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="pushButton">
<property name="minimumSize">
<size>
<width>100</width>
<height>100</height>
</size>
</property>
<property name="maximumSize">
<size>
<width>100</width>
<height>100</height>
</size>
</property>
<property name="text">
<string>PushButton</string>
</property>
<property name="autoRepeatDelay">
<number>100</number>
</property>
</widget>
</item>
</layout>
</widget>
</item>
</layout>
</item>
</layout>
</widget>
</widget>
<resources/>
<connections/>
</ui>
