Создание и открытие нового окна в qt designer при нажатии на виджет menu action
Как в qt designer создать еще одно окно и связать его с главным окном путем нажатия на виджет: например из верхнего menu action или при нажатии на виджет button в главном окне должно открываться новое окно поверх предыдущего?
Ответы (1 шт):
Автор решения: Владимир
→ Ссылка
Всплывающее окно реализовано для вкладки "Программирование". Представлены несколько вариантов. Нужно раскомментировать любой из трех.
main.py
import sys
from PyQt5.QtWidgets import *
from PyQt5.uic import loadUi
class New(QMainWindow):
def __init__(self):
super(New, self).__init__()
loadUi("template1.ui", self)
self.setWindowTitle('New')
self.action.triggered.connect(self.exit_action)
self.action_4.triggered.connect(self.dialog_btn_clicked)
# Свойство автоповтора отключено по умолчанию, поэтому включаем его
self.pushButton.setAutoRepeat(True)
# Установка интервала 500 миллисекунд
self.pushButton.setAutoRepeatInterval(500)
# Вводим нужное число для начала отсчета. Я использовал 0
self.counter = 0
# Добавляем действие к кнопке
self.pushButton.clicked.connect(lambda: self.upClicked())
def dialog_btn_clicked(self):
# Реализованы несколько вариантов вывода окон. Нужно раскомментировать один из вариантов
# res = QMessageBox.information(self, 'Привет!', 'Всем привет!') # 1 вариант (статический медод)
# res = QMessageBox.question(self, 'Пишем', 'Что-то пишем') # 2 вариант (статический медод)
# if res == QMessageBox.Yes: # 2 вариант
# QMessageBox.information(self, 'ДА', 'Вася нажал ДА') # 2 вариант
# elif res == QMessageBox.No: # 2 вариант
# QMessageBox.information(self, 'НЕТ', 'Вася нажал НЕТ') # 2 вариант
mess = QMessageBox() # 3 вариант (динамический метод)
mess.setText('Что-то пишем') # 3 вариант
mess.setDetailedText('Еще что-то пишем') # 3 вариант
# mess.setIcon(QMessageBox.information) # 3 вариант # Закоментировал строку с иконкой. Почему-то выдает ошибку
mess.setWindowTitle('Внимание!') # 3 вариант
mess.setStandardButtons(QMessageBox.Ok | QMessageBox.Cancel) # 3 вариант
mess.exec_() # 3 вариант
# print(res) # для 1 и 2 вариантов
def upClicked(self):
self.label_4.setText(str(self.counter))
self.counter += 1
def exit_action(self):
self.close()
if __name__ == '__main__':
app = QApplication(sys.argv)
window = New()
window.show()
sys.exit(app.exec_())
template1.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>970</width>
<height>734</height>
</rect>
</property>
<property name="windowTitle">
<string>Deqart</string>
</property>
<widget class="QWidget" name="centralwidget">
<widget class="QPushButton" name="pushButton">
<property name="geometry">
<rect>
<x>420</x>
<y>100</y>
<width>125</width>
<height>160</height>
</rect>
</property>
<property name="styleSheet">
<string notr="true">font: 75 72pt "MS Shell Dlg 2";</string>
</property>
<property name="text">
<string>▲</string>
</property>
</widget>
<widget class="QPushButton" name="pushButton_2">
<property name="geometry">
<rect>
<x>420</x>
<y>400</y>
<width>125</width>
<height>160</height>
</rect>
</property>
<property name="styleSheet">
<string notr="true">font: 75 72pt "MS Shell Dlg 2";</string>
</property>
<property name="text">
<string>▼</string>
</property>
</widget>
<widget class="QPushButton" name="pushButton_3">
<property name="geometry">
<rect>
<x>200</x>
<y>270</y>
<width>160</width>
<height>125</height>
</rect>
</property>
<property name="styleSheet">
<string notr="true">font: 75 72pt "MS Shell Dlg 2";</string>
</property>
<property name="text">
<string>◀</string>
</property>
</widget>
<widget class="QPushButton" name="pushButton_4">
<property name="geometry">
<rect>
<x>600</x>
<y>270</y>
<width>160</width>
<height>125</height>
</rect>
</property>
<property name="styleSheet">
<string notr="true">font: 75 72pt "MS Shell Dlg 2";</string>
</property>
<property name="text">
<string>▶</string>
</property>
</widget>
<widget class="QLabel" name="label">
<property name="geometry">
<rect>
<x>360</x>
<y>310</y>
<width>50</width>
<height>50</height>
</rect>
</property>
<property name="font">
<font>
<pointsize>25</pointsize>
</font>
</property>
<property name="text">
<string>OX</string>
</property>
</widget>
<widget class="QLabel" name="label_2">
<property name="geometry">
<rect>
<x>550</x>
<y>310</y>
<width>50</width>
<height>50</height>
</rect>
</property>
<property name="font">
<font>
<pointsize>25</pointsize>
</font>
</property>
<property name="text">
<string>OX</string>
</property>
</widget>
<widget class="QLabel" name="label_3">
<property name="geometry">
<rect>
<x>460</x>
<y>350</y>
<width>50</width>
<height>50</height>
</rect>
</property>
<property name="font">
<font>
<pointsize>25</pointsize>
</font>
</property>
<property name="text">
<string>OY</string>
</property>
</widget>
<widget class="QLabel" name="label_4">
<property name="geometry">
<rect>
<x>460</x>
<y>260</y>
<width>50</width>
<height>50</height>
</rect>
</property>
<property name="font">
<font>
<pointsize>25</pointsize>
</font>
</property>
<property name="text">
<string>OY</string>
</property>
</widget>
</widget>
<widget class="QMenuBar" name="menubar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>970</width>
<height>26</height>
</rect>
</property>
<widget class="QMenu" name="menuFile">
<property name="title">
<string>Файл</string>
</property>
<addaction name="action"/>
</widget>
<widget class="QMenu" name="menu">
<property name="title">
<string>Настройки</string>
</property>
<addaction name="action_2"/>
</widget>
<widget class="QMenu" name="menu_2">
<property name="title">
<string>Программирование</string>
</property>
<addaction name="action_4"/>
</widget>
<widget class="QMenu" name="menu_3">
<property name="title">
<string>Помощь</string>
</property>
</widget>
<addaction name="menuFile"/>
<addaction name="menu"/>
<addaction name="menu_2"/>
<addaction name="menu_3"/>
</widget>
<widget class="QStatusBar" name="statusbar"/>
<action name="action">
<property name="text">
<string>Выход</string>
</property>
</action>
<action name="action_2">
<property name="text">
<string>Настройки</string>
</property>
</action>
<action name="action_4">
<property name="text">
<string>Диалоговое окно</string>
</property>
</action>
</widget>
<resources/>
<connections/>
</ui>