Задать вопрос
@orlatym
я очень тупой

Как создать специальный QWidget?

У меня есть вот такой код
from PySide2.QtCore import (QCoreApplication, QMetaObject, QObject, QPoint,
		QRect, QSize, QUrl, Qt)
	from PySide2.QtGui import (QBrush, QColor, QConicalGradient, QCursor, QFont,
		QFontDatabase, QIcon, QLinearGradient, QPalette, QPainter, QPixmap,
		QRadialGradient)
	from PySide2.QtWidgets import *
	from PySide2.QtCore import QPropertyAnimation, QRect,QTimer
	from PySide2.QtWidgets import QApplication,QWidget
	import sys

	def my_self(self, vertical, into,name_but):
		self.main_winow = QWidget(into)
		self.main_winow.setObjectName(u"main_winow")
		self.verticalLayout = QVBoxLayout(self.main_winow)
		self.verticalLayout.setObjectName(u"verticalLayout")
		self.click_me = QPushButton(self.main_winow)
		self.click_me.setObjectName(name_but)
		self.verticalLayout.addWidget(self.click_me)
		self.mini_window = QWidget(self.main_winow)
		self.mini_window.setObjectName(u"mini_window")
		self.gridLayout = QGridLayout(self.mini_window)
		self.gridLayout.setObjectName(u"gridLayout")
		self.verticalLayout.addWidget(self.mini_window)
		vertical.addWidget(self.main_winow)
		return self


	class Ui_MainWindow(object):
		def setupUi(self, MainWindow):
			if MainWindow.objectName():
				MainWindow.setObjectName(u"MainWindow")
			MainWindow.resize(800, 600)
			MainWindow.setStyleSheet(u"")
			self.centralwidget = QWidget(MainWindow)
			self.centralwidget.setObjectName(u"centralwidget")
			self.widget = QWidget(self.centralwidget)
			self.widget.setObjectName(u"widget")
			self.widget.setGeometry(QRect(120, 30, 191, 461))
			self.verticalLayout_4 = QVBoxLayout(self.widget)
			self.verticalLayout_4.setObjectName(u"verticalLayout_4")
			my_self(self,self.verticalLayout_4,self.widget,u"click_me")
			self.text_label = QLabel(self.mini_window)
			self.text_label.setObjectName(u"text_label")
			self.gridLayout.addWidget(self.text_label, 0, 0, 1, 1)
			self.widget_4 = QWidget(self.widget)
			self.widget_4.setObjectName(u"widget_4")
			self.verticalLayout_2 = QVBoxLayout(self.widget_4)
			self.verticalLayout_2.setObjectName(u"verticalLayout_2")
			self.pushButton_2 = QPushButton(self.widget_4)
			self.pushButton_2.setObjectName(u"pushButton_2")
			self.verticalLayout_2.addWidget(self.pushButton_2)
			self.widget_5 = QWidget(self.widget_4)
			self.widget_5.setObjectName(u"widget_5")
			self.gridLayout_2 = QGridLayout(self.widget_5)
			self.gridLayout_2.setObjectName(u"gridLayout_2")
			self.label_2 = QLabel(self.widget_5)
			self.label_2.setObjectName(u"label_2")
			self.gridLayout_2.addWidget(self.label_2, 0, 0, 1, 1)
			self.verticalLayout_2.addWidget(self.widget_5)
			self.verticalLayout_4.addWidget(self.widget_4)
			self.widget_6 = QWidget(self.widget)
			self.widget_6.setObjectName(u"widget_6")
			self.verticalLayout_3 = QVBoxLayout(self.widget_6)
			self.verticalLayout_3.setObjectName(u"verticalLayout_3")
			self.pushButton_3 = QPushButton(self.widget_6)
			self.pushButton_3.setObjectName(u"pushButton_3")
			self.verticalLayout_3.addWidget(self.pushButton_3)
			self.widget_7 = QWidget(self.widget_6)
			self.widget_7.setObjectName(u"widget_7")
			self.gridLayout_3 = QGridLayout(self.widget_7)
			self.gridLayout_3.setObjectName(u"gridLayout_3")
			self.label_3 = QLabel(self.widget_7)
			self.label_3.setObjectName(u"label_3")
			self.gridLayout_3.addWidget(self.label_3, 0, 0, 1, 1)
			self.verticalLayout_3.addWidget(self.widget_7)
			self.verticalLayout_4.addWidget(self.widget_6)
			self.verticalSpacer = QSpacerItem(20, 188, QSizePolicy.Minimum, QSizePolicy.Expanding)
			self.verticalLayout_4.addItem(self.verticalSpacer)
			self.verticalLayout_4.setStretch(3, 1)
			MainWindow.setCentralWidget(self.centralwidget)
			self.menubar = QMenuBar(MainWindow)
			self.menubar.setObjectName(u"menubar")
			self.menubar.setGeometry(QRect(0, 0, 800, 21))
			MainWindow.setMenuBar(self.menubar)
			self.statusbar = QStatusBar(MainWindow)
			self.statusbar.setObjectName(u"statusbar")
			MainWindow.setStatusBar(self.statusbar)
			self.retranslateUi(MainWindow)
			QMetaObject.connectSlotsByName(MainWindow)
		# setupUi
		def retranslateUi(self, MainWindow):
			MainWindow.setWindowTitle(QCoreApplication.translate("MainWindow", u"MainWindow", None))
			self.click_me.setText(QCoreApplication.translate("MainWindow", u"click_me", None))
			self.text_label.setText(QCoreApplication.translate("MainWindow", u"TextLabel", None))
			self.pushButton_2.setText(QCoreApplication.translate("MainWindow", u"test", None))
			self.label_2.setText(QCoreApplication.translate("MainWindow", u"TextLabel", None))
			self.pushButton_3.setText(QCoreApplication.translate("MainWindow", u"test", None))
			self.label_3.setText(QCoreApplication.translate("MainWindow", u"TextLabel", None))
		# retranslateUi
	class QtMainWindow(QMainWindow, Ui_MainWindow):
		def __init__(self, parent=None):
			super().__init__(parent)
			self.setupUi(self)
			self.click_me.setText('Скрыть `mini_window`')                      # +++
			self.click_me.clicked.connect(self.doAnimation)                    # +++
			QTimer.singleShot(10, self.size_mini_window)                       # +++
		def size_mini_window(self):                                            # +++
			self.s = self.mini_window.size()
		def doAnimation(self):                                                 # +++
			if self.click_me.text() == 'Скрыть `mini_window`':
				self.anim = QPropertyAnimation(self.mini_window, b"size")
				self.anim.setDuration(1500) 
				self.anim.setStartValue(self.s)
				self.anim.setEndValue(QSize(self.s.width(), 0))
				self.anim.start()
				self.click_me.setText('Показать `mini_window`')
			else:
				self.anim = QPropertyAnimation(self.mini_window, b"size")
				self.anim.setDuration(1500) 
				self.anim.setStartValue(QSize(self.s.width(), 0))
				self.anim.setEndValue(self.s)
				self.anim.start()
				self.click_me.setText('Скрыть `mini_window`')


	StyleSheet = """
	#centralwidget QWidget QWidget,
	#centralwidget QWidget QWidget QWidget QWidget,
	#centralwidget QWidget QWidget QWidget QWidget QWidget QWidget,
	#centralwidget QWidget QWidget QWidget QWidget QWidget QWidget QWidget QWidget,
	#centralwidget QWidget QWidget QWidget QWidget QWidget QWidget QWidget QWidget QWidget QWidget{
	background:#000;
	color:#fff;
	}


	#centralwidget QWidget,
	#centralwidget QWidget QWidget QWidget,
	#centralwidget QWidget QWidget QWidget QWidget QWidget,
	#centralwidget QWidget QWidget QWidget QWidget QWidget QWidget QWidget,
	#centralwidget QWidget QWidget QWidget QWidget QWidget QWidget QWidget QWidget QWidget{
	background:#fff;
	color:#000;
	}
	"""

	if __name__ == '__main__':
		app = QApplication(sys.argv)

		app.setStyleSheet(StyleSheet)                    # <----- StyleSheet

		window = QtMainWindow()
		window.show()
		sys.exit(app.exec_()

Я пытаюсь создать функцию Widget с кастомными настройками во внутрь которого я могу засунуть любой другой виджет, текст, кнопки и после по нажатию кнопки скрыть его
IZ2Kr.gif
Но у меня код работает неправильно ведь я не знаю как избавится от повтора имен и как обновлять комновку всего шаблона во время анимации
  • Вопрос задан
  • 96 просмотров
Подписаться 1 Простой Комментировать
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Похожие вопросы