|
楼主 |
发表于 2024-4-14 12:54:11
|
显示全部楼层
现有代码如下 :
import sys
import random
from PySide6 import QtCore, QtGui, QtWidgets
from PySide6.QtWidgets import (QApplication, QMainWindow, QLabel, QLineEdit, QPushButton, QDateTimeEdit, QFrame,
QMessageBox, QVBoxLayout, QHBoxLayout, QWidget, QProgressBar, QPlainTextEdit,
QFormLayout, QRadioButton, QComboBox)
from PySide6.QtCore import Qt, QEvent, Signal, QRect, QSize, QCoreApplication, QMetaObject, QDateTime
from PySide6.QtGui import QFont, QColor
import requests
import mysql.connector
import pymongo
class BaseWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("欢迎使用")
self.error_count = 0
# 设置窗体大小和背景颜色
self.setFixedSize(1440, 900)
# 设置ESC事件
self.setWindowFlags(Qt.CustomizeWindowHint | Qt.WindowCloseButtonHint)
self.setFocusPolicy(Qt.StrongFocus)
self.installEventFilter(self)
# 设置退出按钮
self.exit_button = QPushButton("退出", self)
self.exit_button.clicked.connect(self.confirm_exit)
# 创建一个消息框
msg_box = QMessageBox()
# 设置标题的字体和颜色
title_font = QFont('隶书', 20, QFont.Weight.Bold)
title_color = QColor(255, 0, 0) # 红色
msg_box.setWindowTitle('敬告')
msg_box.setStyleSheet(f"QLabel#qt_msgbox_label{{ color: {title_color.name()}; }}")
msg_box.setText(f'<span style="color:{title_color.name()}">{msg_box.text()}</span>')
msg_box.setFont(title_font)
# 设置文本的字体和颜色
text_font = QFont('隶书', 28)
text_color = QColor(0, 0, 255) # 蓝色
msg_box.setText('敬 告:\n'
# 创建布局
layout = QVBoxLayout(self)
layout1 = QHBoxLayout(self)
layout2 = QHBoxLayout(self)
layout3 = QHBoxLayout(self)
layout4 = QHBoxLayout(self)
# 创建并居中显示欢迎和使用本系统标签
welcome_label = QLabel("欢迎使用")
# 创建登录界面
username_label = QLabel("用户名")
font = QtGui.QFont()
。。。。各种控件布局
def login(self):
username = self.username_edit.text()
password = self.password_edit.text()
# TODO: 判断用户名和密码是否正确,以及错误次数是否满足条件
if username == "1" and password == "1":
self.start_progress_bar()
else:
self.error_count += 1
self.username_edit.clear()
self.password_edit.clear()
if self.error_count >= 3:
QMessageBox.critical(self, "错误", "错误次数过多!!")
self.close()
else:
QMessageBox.warning(self, "错误", "无效用户名或密码!!")
def start_progress_bar(self):
self.progress_bar.setValue(0)
self.progress_bar.show()
self.timer = self.startTimer(100)
def stop_progress_bar(self):
self.killTimer(self.timer)
def open_window_r(self):
self.scroll_window = Region_select_window()
self.scroll_window.close_signal.connect(self.open_window_r)
self.hide()
self.scroll_window.show()
def eventFilter(self, obj, event):
if event.type() == QEvent.KeyPress and event.key() == Qt.Key_Escape:
self.confirm_exit()
return True
return super().eventFilter(obj, event)
class Region_select_window(BaseWindow):
close_signal = Signal()
def __init__(self):
super().__init__()
# if not Form.objectName():
# Form.setObjectName(u"Form")
self.setWindowModality(Qt.WindowModal)
self.setEnabled(True)
self.resize(800, 600)
self.retranslateUi()
QMetaObject.connectSlotsByName(self)
# 设置ESC事件
self.setWindowFlags(Qt.CustomizeWindowHint | Qt.WindowCloseButtonHint)
self.setFocusPolicy(Qt.StrongFocus)
self.installEventFilter(self)
# 设置退出按钮
self.exit_button = QPushButton("退出", self)
self.exit_button.clicked.connect(self.confirm_exit)
def retranslateUi(self):
self.setWindowTitle(QCoreApplication.translate("self", u"main", None))
if __name__ == "__main__":
app = QApplication(sys.argv)
window = BaseWindow()
window.show()
sys.exit(app.exec())
现在有问题是:1、class BaseWindow(QMainWindow)会在打开class Region_select_window(BaseWindow)时运行两次,
class BaseWindow的内容还会显示在class Region_select_window的后面。。。
2、如果我把class Region_select_window(BaseWindow)中的(BaseWindow)改为(QMainWindow),
即class Region_select_window(QMainWindow)后,就能正常运行了。
但我实际需求却是需要class Region_select_window能够继承class BaseWindow(QMainWindow)的ESC事件和退出按钮....
请问需要怎么解决class BaseWindow(QMainWindow)运行两次的问题? |
|