Как вставить картинку и текст в QTabWidget?
Есть два окна:
- в первом несколько табов и кнопка, при нажатии на которую открывается вторая форма,
- в которой две кнопки, при нажатии на первую - открывается выбор картинки (
QFileDialog), при нажатии на вторую -QInputDialog, который просит ввести текст.
Как выбранную картинку и текст вставить в выбранный таб первого окошка?
Во втором окне можно указать размер и положение( x и y) картинки и текста.
from PyQt5.QtWidgets import QWidget, QMainWindow, QFileDialog
from PyQt5 import uic
import sys
from PyQt5.QtWidgets import QApplication
class FirstForm(QMainWindow):
def __init__(self):
super().__init__()
uic.loadUi('form1.ui', self)
self.pushButton.clicked.connect(self.open)
self.secondform = SecondForm()
self.secondform.btn2_2.clicked.connect(self.addimage)
def addimage(self):
self.fname = QFileDialog.getOpenFileName(self.secondform, 'Выбрать картинку', '')[0]
def open(self):
self.secondform.show()
class SecondForm(QWidget):
def __init__(self):
super().__init__()
uic.loadUi('form2.ui', self)
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = FirstForm()
ex.show()
sys.exit(app.exec())
uic главного окна:
<?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>800</width>
<height>600</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralwidget">
<widget class="QTabWidget" name="tabWidget">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>541</width>
<height>471</height>
</rect>
</property>
<widget class="QWidget" name="tab">
<attribute name="title">
<string>Tab 1</string>
</attribute>
</widget>
<widget class="QWidget" name="tab_2">
<attribute name="title">
<string>Tab 2</string>
</attribute>
</widget>
</widget>
<widget class="QPushButton" name="pushButton">
<property name="geometry">
<rect>
<x>60</x>
<y>480</y>
<width>181</width>
<height>71</height>
</rect>
</property>
<property name="text">
<string>Открыть второе окно</string>
</property>
</widget>
</widget>
<widget class="QMenuBar" name="menubar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>800</width>
<height>21</height>
</rect>
</property>
</widget>
<widget class="QStatusBar" name="statusbar"/>
</widget>
<resources/>
<connections/>
</ui>
uic второго:
<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="QPushButton" name="btn2">
<property name="geometry">
<rect>
<x>50</x>
<y>40</y>
<width>101</width>
<height>31</height>
</rect>
</property>
<property name="text">
<string>текст</string>
</property>
</widget>
<widget class="QPushButton" name="btn2_2">
<property name="geometry">
<rect>
<x>50</x>
<y>130</y>
<width>101</width>
<height>31</height>
</rect>
</property>
<property name="text">
<string>картинка</string>
</property>
</widget>
<widget class="QLabel" name="label">
<property name="geometry">
<rect>
<x>190</x>
<y>40</y>
<width>61</width>
<height>21</height>
</rect>
</property>
<property name="text">
<string>Положение</string>
</property>
</widget>
<widget class="QLabel" name="label_2">
<property name="geometry">
<rect>
<x>290</x>
<y>40</y>
<width>61</width>
<height>21</height>
</rect>
</property>
<property name="text">
<string>Размеры</string>
</property>
</widget>
<widget class="QLineEdit" name="lineEdit">
<property name="geometry">
<rect>
<x>200</x>
<y>70</y>
<width>31</width>
<height>20</height>
</rect>
</property>
</widget>
<widget class="QLineEdit" name="lineEdit_2">
<property name="geometry">
<rect>
<x>200</x>
<y>130</y>
<width>31</width>
<height>20</height>
</rect>
</property>
</widget>
<widget class="QLineEdit" name="lineEdit_3">
<property name="geometry">
<rect>
<x>290</x>
<y>70</y>
<width>31</width>
<height>20</height>
</rect>
</property>
</widget>
<widget class="QLineEdit" name="lineEdit_4">
<property name="geometry">
<rect>
<x>290</x>
<y>130</y>
<width>31</width>
<height>20</height>
</rect>
</property>
</widget>
</widget>
<resources/>
<connections/>
</ui>
Ответы (1 шт):
Автор решения: S. Nick
→ Ссылка
Bставить картинку и текст в QTabWidget можно примерно так:
main.py
import sys
from PyQt5 import QtWidgets, QtGui, QtCore, uic
from PyQt5.Qt import *
class FirstForm(QMainWindow):
def __init__(self):
super().__init__()
uic.loadUi('q1475435_form1.ui', self)
self.pushButton.clicked.connect(self.open)
self.labelImage = QLabel(self.tab)
self.textInput = QLabel(self)
self.secondform = SecondForm()
self.secondform.signalOk.connect(self._insert)
def _insert(self):
pixmap = self.secondform.labelImage.pixmap()
self.labelImage.setPixmap(pixmap)
self.textInput.setText(self.secondform.textInput.text())
try:
x1 = int(self.secondform.lineEdit_2.text())
y1 = int(self.secondform.lineEdit_4.text())
x2 = int(self.secondform.lineEdit.text())
y2 = int(self.secondform.lineEdit_3.text())
except:
x1 = 20
y1 = 20
x2 = 70
y2 = 180
self.labelImage.move(x1, y1)
self.labelImage.adjustSize()
self.textInput.move(x2, y2)
self.textInput.adjustSize()
def open(self):
self.secondform.show()
class SecondForm(QWidget):
signalOk = pyqtSignal()
def __init__(self):
super().__init__()
uic.loadUi('q1475435_form2.ui', self)
self.btn2.clicked.connect(self.getTextInputDialog)
self.btn2_2.clicked.connect(self.addimage)
self.textInput = QLabel(self)
self.textInput.setGeometry(QtCore.QRect(190, 20, 61, 21))
self.labelImage = QLabel(self)
self.labelImage.setGeometry(QtCore.QRect(100, 170, 200, 125))
self.btnOk = QtWidgets.QPushButton('Вставить \nв первое \nокошко', self)
self.btnOk.setGeometry(QtCore.QRect(330, 230, 60, 60))
self.btnOk.clicked.connect(self.signalOk.emit)
def addimage(self):
fname, _ = QFileDialog.getOpenFileName(
self,
'Выбрать картинку',
'',
'PNG Files (*.png);;JPG Files (*.jpg)',
)
if fname:
self.labelImage.setPixmap(QtGui.QPixmap(fname).scaled(200, 125))
def getTextInputDialog(self):
text, okPressed = QtWidgets.QInputDialog.getText(
None,
"Get text",
"Введите текст:",
QtWidgets.QLineEdit.Normal,
"")
if okPressed and text:
self.textInput.setText(text)
self.textInput.adjustSize()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = FirstForm()
ex.show()
sys.exit(app.exec())
q1475435_form1.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>800</width>
<height>600</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralwidget">
<widget class="QTabWidget" name="tabWidget">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>541</width>
<height>471</height>
</rect>
</property>
<widget class="QWidget" name="tab">
<attribute name="title">
<string>Tab 1</string>
</attribute>
</widget>
<widget class="QWidget" name="tab_2">
<attribute name="title">
<string>Tab 2</string>
</attribute>
</widget>
</widget>
<widget class="QPushButton" name="pushButton">
<property name="geometry">
<rect>
<x>60</x>
<y>480</y>
<width>181</width>
<height>71</height>
</rect>
</property>
<property name="text">
<string>Открыть второе окно</string>
</property>
</widget>
</widget>
<widget class="QMenuBar" name="menubar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>800</width>
<height>21</height>
</rect>
</property>
</widget>
<widget class="QStatusBar" name="statusbar"/>
</widget>
<resources/>
<connections/>
</ui>
q1475435_form2.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="QPushButton" name="btn2">
<property name="geometry">
<rect>
<x>50</x>
<y>40</y>
<width>101</width>
<height>31</height>
</rect>
</property>
<property name="text">
<string>текст</string>
</property>
</widget>
<widget class="QPushButton" name="btn2_2">
<property name="geometry">
<rect>
<x>50</x>
<y>130</y>
<width>101</width>
<height>31</height>
</rect>
</property>
<property name="text">
<string>картинка</string>
</property>
</widget>
<widget class="QLabel" name="label">
<property name="geometry">
<rect>
<x>190</x>
<y>40</y>
<width>61</width>
<height>21</height>
</rect>
</property>
<property name="text">
<string>Положение</string>
</property>
</widget>
<widget class="QLabel" name="label_2">
<property name="geometry">
<rect>
<x>290</x>
<y>40</y>
<width>61</width>
<height>21</height>
</rect>
</property>
<property name="text">
<string>Размеры</string>
</property>
</widget>
<widget class="QLineEdit" name="lineEdit">
<property name="geometry">
<rect>
<x>200</x>
<y>70</y>
<width>31</width>
<height>20</height>
</rect>
</property>
</widget>
<widget class="QLineEdit" name="lineEdit_2">
<property name="geometry">
<rect>
<x>200</x>
<y>130</y>
<width>31</width>
<height>20</height>
</rect>
</property>
</widget>
<widget class="QLineEdit" name="lineEdit_3">
<property name="geometry">
<rect>
<x>290</x>
<y>70</y>
<width>31</width>
<height>20</height>
</rect>
</property>
</widget>
<widget class="QLineEdit" name="lineEdit_4">
<property name="geometry">
<rect>
<x>290</x>
<y>130</y>
<width>31</width>
<height>20</height>
</rect>
</property>
</widget>
</widget>
<resources/>
<connections/>
</ui>
