I have a QMainWindow that have a button, when clicked this button, another widget pops up. This widget have a button, when clicked this button an alert message pops up. When I press 'OK' on that message button, only QMessageBox is closing, small widget is still open. I want to close that widget when I press 'OK' on that message button. I couldn't figure out how can I do this. Here is my code;
我有一个有按钮的QMainWindow,当单击这个按钮时,会弹出另一个小部件。这个小部件有一个按钮,当单击这个按钮时,会弹出一条警报消息。当我在消息按钮上按“OK”时,只有QMessageBox关闭,小部件仍然打开。当我在消息按钮上按“OK”时,我想关闭这个小部件。我不知道该怎么做。这是我的代码;
from PyQt5.QtWidgets import (QMessageBox,QApplication, QWidget, QToolTip, QPushButton,
QDesktopWidget, QMainWindow, QAction, qApp, QToolBar, QVBoxLayout,
QComboBox,QLabel,QLineEdit,QGridLayout,QMenuBar,QMenu,QStatusBar,
QTextEdit,QDialog,QFrame,QProgressBar,QHBoxLayout
)
from PyQt5 import QtCore, QtWidgets, QtGui
from PyQt5.QtGui import QIcon,QFont,QPixmap,QPalette,QWindow
from PyQt5.QtCore import QCoreApplication, Qt,QBasicTimer, QTimer,QPoint
import PyQt5.QtWidgets,PyQt5.QtCore
import time,random,subprocess,sys,json
class cssden(QMainWindow):
def __init__(self):
super().__init__()
self.mwidget = QMainWindow(self)
self.setWindowFlags(QtCore.Qt.FramelessWindowHint)
self.setFixedSize(600,500)
#Other widget button
self.owidget = QPushButton(self)
self.owidget.clicked.connect(self.second_widget)
self.show()
#other widget
#I want to destroy this widget when I press 'OK' on the QMessageBox
def second_widget(self):
self.w_window = QWidget()
self.w_window.setGeometry(650,300,600,300)
self.w_window.setStyleSheet("background-color: lightblue")
self.w_button = QPushButton(self.w_window)
self.w_button.setText("Alert")
self.w_button.clicked.connect(self.alert)
self.w_window.show()
#alert from second widget
def alert(self):
QMessageBox.about(self.w_window,"Alert","Alert message")
app = QApplication(sys.argv)
app.setStyleSheet("QMainWindow{background-color: rgb(30,30,30);border: 1px solid black}")
ex = cssden()
sys.exit(app.exec_())
I tried to connect them ('OK' button of QMessageBox and last widget) but I couldn't do it very well and I'm really confused.
我尝试连接它们(QMessageBox和last widget的'OK'按钮),但是我做得不好,我真的很困惑。
1 个解决方案
#1
0
I figured out the solution by myself, just make a QMessageBox(no inherit), then find the result and check if the result is something you want, close()
the widget.
我自己找到了解决方案,只需创建一个QMessageBox(无继承),然后找到结果并检查结果是否是您想要的,close()小部件。
self.result1 = QMessageBox(QMessageBox.Information,"Alert","Alert message",QMessageBox.Ok)
self.result1.setGeometry(500,500,500,500)
self.result1.show()
result = self.result1.exec_()
if result == 1024:
self.w_window.close()
I used 'OK' button so when I printed result
the value was 1024
, so before you check which button clicked (if you use yes|no buttons) print the result first then find the value, then do your job.
我使用了“OK”按钮,所以当我打印结果时,值是1024,所以在您检查哪个按钮被单击(如果您使用yes|no按钮)之前,先打印结果,然后找到值,然后执行您的任务。
#1
0
I figured out the solution by myself, just make a QMessageBox(no inherit), then find the result and check if the result is something you want, close()
the widget.
我自己找到了解决方案,只需创建一个QMessageBox(无继承),然后找到结果并检查结果是否是您想要的,close()小部件。
self.result1 = QMessageBox(QMessageBox.Information,"Alert","Alert message",QMessageBox.Ok)
self.result1.setGeometry(500,500,500,500)
self.result1.show()
result = self.result1.exec_()
if result == 1024:
self.w_window.close()
I used 'OK' button so when I printed result
the value was 1024
, so before you check which button clicked (if you use yes|no buttons) print the result first then find the value, then do your job.
我使用了“OK”按钮,所以当我打印结果时,值是1024,所以在您检查哪个按钮被单击(如果您使用yes|no按钮)之前,先打印结果,然后找到值,然后执行您的任务。