如何显示另一个窗口

如何显示另一个窗口

本文介绍了如何显示另一个窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

项目发生在登录和登录.

The project occurs loging in and signing in.

我正在尝试从 registerwindow 过渡到 mainwindow,当我们提交时,窗口会自动过渡到 mainwindow.只有一种方法可以做到这一点(至少对我而言)我必须导入两个名为 mainwindow.py 和 register.py 的 python 文档,顺便说一下,它们在同一个文档中.

Im trying a transition from registerwindow to mainwindow and when we submit the window is automatically transit to mainwindow. There is only way to do this (at least for me) i have to import two python doc which named mainwindow.py and register.py, they are in same doc by the way.

这是主菜单.py

from PyQt5 import QtCore,QtGui,QtWidgets

from window.register import Ui_Form

class Ui_MainWindow(object):


def login(self):

    self.window = QtWidgets.QWidget()
    self.ui = Ui_Form()
    self.ui.setupUi(self.window)
    self.window.show()
    MainWindow.hide()

这是 register.py

and this is register.py

from PyQt5 import QtCore, QtGui, QtWidgets

from window.mainmenu import Ui_MainWindow

import sqlite3

class Ui_Form(object):
def submit(self):
    sorgu2 = "Select * From users where nickname = ?"
    sorgu = "INSERT INTO users values(?,?)"
    self.cursor.execute(sorgu,(self.lineEdit.text(),self.lineEdit.text()))
    self.connect.commit()
    Form.hide()
    self.window2 = QtWidgets.QMainWindow()
    self.ui2 = Ui_MainWindow()
    self.ui2.setupUi(self.window2)
    self.window2.show()

它应该是当我点击按钮时注册窗口将被隐藏并显示主菜单窗口.主菜单相同,但正好相反

Its supposed to be when i clicked to the button the register window will be hidden and mainmenu window will be show. Same thing for the mainmenu but the direct opposite

我知道我在做循环依赖导入,但除了相互导入之外别无他法

I know i am doing circular dependent imports but there is no other way but importing them to each other

推荐答案

如果第二个窗口是 QDialog 那么你可以隐藏主窗口,使用 exec() for QDialog 和主窗口会一直等到你关闭QDialog,当它返回到主窗口时你可以再次显示它.

If second window will be QDialog then you can hide main window, use exec() for QDialog and main window will wait till you close QDialog, and when it returns to main window then you can show it again.

from PyQt5 import QtWidgets


class MainWindow(QtWidgets.QWidget):

    def __init__(self):
        super().__init__()

        self.button = QtWidgets.QPushButton("Show Second Window", self)
        self.button.clicked.connect(self.show_second_window)
        layout = QtWidgets.QVBoxLayout(self)
        layout.addWidget(self.button)

        self.show()

    def show_second_window(self):
        self.hide() # hide main window

        self.second = SecondWindow()
        self.second.exec() # will wait till you close second window

        self.show() # show main window again


class SecondWindow(QtWidgets.QDialog): # it has to be dialog

    def __init__(self):
        super().__init__()

        self.button = QtWidgets.QPushButton("Close It", self)
        self.button.clicked.connect(self.show_second_window)
        layout = QtWidgets.QVBoxLayout(self)
        layout.addWidget(self.button)

        self.show()

    def show_second_window(self):
        self.close() # go back to main window


app = QtWidgets.QApplication([])
main = MainWindow()
app.exec()

另一种流行的方法是创建两个包含所有内容的小部件并替换一个窗口中的小部件.


The other popular method is to create two widgets with all contents and replace widgets in one window.

from PyQt5 import QtWidgets


class MainWidget(QtWidgets.QWidget):

    def __init__(self, parent):
        super().__init__()

        self.parent = parent

        self.button = QtWidgets.QPushButton("Show Second Window", self)
        self.button.clicked.connect(self.show_second_window)
        layout = QtWidgets.QVBoxLayout(self)
        layout.addWidget(self.button)

        self.show()

    def show_second_window(self):
        self.close()
        self.parent.set_content("Second")


class SecondWidget(QtWidgets.QWidget):

    def __init__(self, parent):
        super().__init__()

        self.parent = parent

        self.button = QtWidgets.QPushButton("Close It", self)
        self.button.clicked.connect(self.show_second_window)
        layout = QtWidgets.QVBoxLayout(self)
        layout.addWidget(self.button)

        self.show()

    def show_second_window(self):
        self.close()
        self.parent.set_content("Main")


class MainWindow(QtWidgets.QWidget):

    def __init__(self):
        super().__init__()

        self.layout = QtWidgets.QVBoxLayout(self)

        self.set_content("Main")

        self.show()

    def set_content(self, new_content):
        if new_content == "Main":
            self.content = MainWidget(self)
            self.layout.addWidget(self.content)
        elif new_content == "Second":
            self.content = SecondWidget(self)
            self.layout.addWidget(self.content)



app = QtWidgets.QApplication([])
main = MainWindow()
app.exec()

使用 QStackedLayout

from PyQt5 import QtWidgets


class FirstWidget(QtWidgets.QWidget):

    def __init__(self, parent=None):
        super().__init__(parent=parent)

        layout = QtWidgets.QVBoxLayout(self)

        self.button = QtWidgets.QPushButton("Show Second Stack", self)
        self.button.clicked.connect(self.change_stack)

        layout.addWidget(self.button)

    def change_stack(self):
        self.parent().stack.setCurrentIndex(1)


class SecondWidget(QtWidgets.QWidget):

    def __init__(self, parent=None):
        super().__init__(parent=parent)

        layout = QtWidgets.QVBoxLayout(self)

        self.button = QtWidgets.QPushButton("Show First Stack", self)
        self.button.clicked.connect(self.change_stack)

        layout.addWidget(self.button)

    def change_stack(self):
        self.parent().stack.setCurrentIndex(0)


class MainWindow(QtWidgets.QWidget):

    def __init__(self):
        super().__init__()

        self.stack = QtWidgets.QStackedLayout(self)

        self.stack1 = FirstWidget(self)
        self.stack2 = SecondWidget(self)

        self.stack.addWidget(self.stack1)
        self.stack.addWidget(self.stack2)

        self.show()



app = QtWidgets.QApplication([])
main = MainWindow()
app.exec()

这篇关于如何显示另一个窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 03:09