Python 如何显示另一个窗口
项目在登录和登录时发生 我正在尝试从registerwindow转换到mainwindow,当我们提交时,窗口会自动转换到mainwindow。只有一种方法可以做到这一点(至少对我来说),我必须导入两个名为mainwindow.py和register.py的python文档,顺便说一句,它们在同一个文档中 这是main menu.pyPython 如何显示另一个窗口,python,pyqt5,Python,Pyqt5,项目在登录和登录时发生 我正在尝试从registerwindow转换到mainwindow,当我们提交时,窗口会自动转换到mainwindow。只有一种方法可以做到这一点(至少对我来说),我必须导入两个名为mainwindow.py和register.py的python文档,顺便说一句,它们在同一个文档中 这是main menu.py from PyQt5 import QtCore,QtGui,QtWidgets from window.register import Ui_Form cl
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
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()
它应该是当我点击按钮,注册窗口将被隐藏,主菜单窗口将显示。主菜单也一样,但正好相反
我知道我在做循环相关的导入,但是除了将它们相互导入之外没有其他方法了如果第二个窗口是
QDialog
,那么您可以隐藏主窗口,使用exec()
进行QDialog
,主窗口将等待您关闭QDialog
,当它返回到主窗口时,您可以再次显示它
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()
另一种流行的方法是创建两个包含所有内容的小部件,并在一个窗口中替换小部件
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()
如果第二个窗口是
QDialog
,则您可以隐藏主窗口,对QDialog
使用exec()
,主窗口将等待您关闭QDialog
,当它返回主窗口时,您可以再次显示它
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()
另一种流行的方法是创建两个包含所有内容的小部件,并在一个窗口中替换小部件
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()
在另一个文件中,您可以创建创建第一个窗口的循环,当您退出第一个窗口时,它将销毁它并创建第二个窗口。当你们关闭第二个窗口时,它会转到循环的开始并显示第一个窗口。或者主窗口应该隐藏自己(而不是销毁),并显示第二个窗口。当第二个窗口结束它的工作,然后它应该关闭它,然后代码应该回到主窗口,这应该破坏第二个窗口,并再次显示主窗口。第二个窗口不应创建第一个窗口,但应退出以返回主窗口。在另一个文件中,您可以创建创建第一个窗口的循环,当您退出第一个窗口时,它将销毁该窗口并创建第二个窗口。当你们关闭第二个窗口时,它会转到循环的开始并显示第一个窗口。或者主窗口应该隐藏自己(而不是销毁),并显示第二个窗口。当第二个窗口结束它的工作,然后它应该关闭它,然后代码应该回到主窗口,这应该破坏第二个窗口,并再次显示主窗口。第二个窗口不应该创建第一个窗口,但它应该退出以返回主窗口。为什么我们使用self.parent=parent以及self.parent.set_content(“第二个”)是什么。这一行还有一个问题app=qtwidts.QApplication([])为什么有“[]'您可以在控制台中运行带有参数的脚本-
python script.py arg1 arg2
,这些参数将作为列表sys.argv
,您可以将其发送到QApplication(sys.argv)
。通过这种方式,您可以在启动脚本时更改Qt模块中的一些设置。我记得,通过这种方式,您可以更改Qt使用的主题。但我不需要这些参数,所以我将空列表而不是sys.argv
。我不能在没有任何列表的情况下使用QApplication()
,因为它会显示错误。在第二个示例中,我只有一个窗口,我在这个窗口中更改了小部件。我在主窗口中有函数set\u content
来执行此操作。我将parent
发送给小部件,这样他们就可以访问主窗口和set_content
,当我按下小部件中的按钮时,他们可以运行它来更改内容。python中是否有set_content()函数?还是属于pyqt5?当我们这样做的时候,self.parent.set_content(“Main”)是self.parent等于“Main”,而set_content(“Main”)是属于MainWindow的,您是如何转换它的。我真的不明白:(为什么我们使用self.parent=parent,什么是self.parent.set_content(“Second”)。这行还有一个问题app=qtwidts.QApplication([])为什么有“[]'您可以在控制台中运行带有参数的脚本-python script.py arg1 arg2
,该参数将作为列表sys.argv
,您可以将其发送到QApplication(sys.argv)
。这种方式可以在启动脚本时更改Qt模块中的一些设置。我记得这种方式可以更改Qt使用的主题。但我不需要这些参数,所以我将空列表而不是sys.argv
。我不能使用QApplication()
没有任何列表,因为它会显示错误。在第二个示例中,我只有一个窗口,我在这个窗口中更改小部件。我在主窗口中有函数set\u content
来执行此操作。我将parent
发送给小部件,以便它们可以访问主窗口和set\u content
,并且它们可以运行它来更改内容当我按下widget.com中的按钮时,python中是否有set_content()函数?或者它属于pyqt5?当我们这样做时-self.parent.set_content(“Main”)-self.parent等于“Main”,而set_content(“Main”)属于MainWindow,你是如何转换它的。我真的不明白:(