pyqt5点击主窗口,子窗口不躲到主窗口后面
时间: 2023-05-20 16:06:17 浏览: 85
可以通过设置子窗口的属性来实现,具体可以使用setWindowFlags()函数,将子窗口的属性设置为Qt::Dialog,这样子窗口就会一直在主窗口的前面显示,不会被主窗口遮挡。以下是示例代码:
```
from PyQt5.QtWidgets import QApplication, QMainWindow, QDialog
from PyQt5.QtCore import Qt
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.setWindowTitle('Main Window')
self.setGeometry(100, 100, 500, 500)
self.setCentralWidget(QDialog(self))
class SubWindow(QDialog):
def __init__(self, parent=None):
super().__init__(parent)
self.initUI()
def initUI(self):
self.setWindowTitle('Sub Window')
self.setGeometry(150, 150, 300, 300)
self.setWindowFlags(Qt.Dialog)
if __name__ == '__main__':
app = QApplication([])
main_window = MainWindow()
sub_window = SubWindow(main_window)
main_window.show()
sub_window.show()
app.exec_()
```
在这个示例中,我们创建了一个主窗口和一个子窗口,将子窗口的父窗口设置为主窗口,然后在子窗口的initUI()函数中设置了窗口的标题、大小和属性。最后在主程序中分别显示了主窗口和子窗口。运行程序后,点击主窗口时,子窗口不会被遮挡,一直在主窗口的前面显示。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)