[{"data":1,"prerenderedAt":613},["ShallowReactive",2],{"post-2025-08-21-pyqt5":3},{"_path":4,"_dir":5,"_draft":6,"_partial":6,"_locale":7,"title":8,"description":9,"date":10,"tags":11,"body":15,"_type":607,"_id":608,"_source":609,"_file":610,"_stem":611,"_extension":612},"\u002Fblog\u002F2025-08-21-pyqt5","blog",false,"","PyQt5-多窗口设计","PyQt5多窗口设计教程，窗口跳转、数据传递与窗口模式控制。","2025-08-21",[12,13,14],"Python","PyQt5","GUI",{"type":16,"children":17,"toc":602},"root",[18,26,33,37,42,51,89,98,103,111,148,154,157,192,285,291,294,299,336,350,385,408,454,525,554,568,588,596],{"type":19,"tag":20,"props":21,"children":22},"element","p",{},[23],{"type":24,"value":25},"text","一个完整的项目一般都是由多个窗口组成的，此时就需要对多窗口设计有所了解。多窗口即向项目中添加多个窗口，在这些窗口中实现不同的功能。下面对多窗口的建立、启动以及如何关联多个窗口进行讲解。",{"type":19,"tag":27,"props":28,"children":30},"h2",{"id":29},"_1多窗口的建立",[31],{"type":24,"value":32},"1  多窗口的建立",{"type":19,"tag":34,"props":35,"children":36},"hr",{},[],{"type":19,"tag":20,"props":38,"children":39},{},[40],{"type":24,"value":41},"多窗口的建立即是向某个项目中添加多个窗口。",{"type":19,"tag":20,"props":43,"children":44},{},[45],{"type":19,"tag":46,"props":47,"children":48},"strong",{},[49],{"type":24,"value":50},"实例 创建并打开多窗口",{"type":19,"tag":20,"props":52,"children":53},{},[54,56,63,65,71,73,79,81,87],{"type":24,"value":55},"在Qt Designer设计器的菜单栏中，选择",{"type":19,"tag":57,"props":58,"children":60},"code",{"className":59},[],[61],{"type":24,"value":62},"文件",{"type":24,"value":64},"→",{"type":19,"tag":57,"props":66,"children":68},{"className":67},[],[69],{"type":24,"value":70},"新建",{"type":24,"value":72},"菜单项，弹出",{"type":19,"tag":57,"props":74,"children":76},{"className":75},[],[77],{"type":24,"value":78},"新建窗体",{"type":24,"value":80},"对话框，选择一个模板，单击",{"type":19,"tag":57,"props":82,"children":84},{"className":83},[],[85],{"type":24,"value":86},"创建",{"type":24,"value":88},"按钮，如图所示。",{"type":19,"tag":20,"props":90,"children":91},{},[92],{"type":19,"tag":93,"props":94,"children":97},"img",{"alt":95,"src":96},"Pasted image 20250827151225","\u002Fimages\u002Fblog\u002Fpyqt5\u002FPasted%20image%2020250827151225.png",[],{"type":19,"tag":20,"props":99,"children":100},{},[101],{"type":24,"value":102},"重复执行以上步骤，即可添加多个窗口，例如，向项目中添加4个窗口的效果如图所示。",{"type":19,"tag":20,"props":104,"children":105},{},[106],{"type":19,"tag":93,"props":107,"children":110},{"alt":108,"src":109},"Pasted image 20250827151427","\u002Fimages\u002Fblog\u002Fpyqt5\u002FPasted%20image%2020250827151427.png",[],{"type":19,"tag":112,"props":113,"children":114},"blockquote",{},[115],{"type":19,"tag":20,"props":116,"children":117},{},[118,123,125,131,133,139,141,146],{"type":19,"tag":46,"props":119,"children":120},{},[121],{"type":24,"value":122},"Info:",{"type":24,"value":124}," 说明\n在Qt Designer设计器中添加多个窗口后，在保存时，需要分别将鼠标焦点定位到要保存的窗口上，单独为每个窗口进行保存；而在将",{"type":19,"tag":57,"props":126,"children":128},{"className":127},[],[129],{"type":24,"value":130},".ui",{"type":24,"value":132},"文件转换为",{"type":19,"tag":57,"props":134,"children":136},{"className":135},[],[137],{"type":24,"value":138},".py",{"type":24,"value":140},"文件时，也需要分别选中每个",{"type":19,"tag":57,"props":142,"children":144},{"className":143},[],[145],{"type":24,"value":130},{"type":24,"value":147},"文件，单独进行转换。",{"type":19,"tag":27,"props":149,"children":151},{"id":150},"_2设置启动窗口",[152],{"type":24,"value":153},"2  设置启动窗口",{"type":19,"tag":34,"props":155,"children":156},{},[],{"type":19,"tag":20,"props":158,"children":159},{},[160,162,167,169,175,177,183,185,190],{"type":24,"value":161},"在项目中添加了多个窗口以后，如果要调试程序，必须要设置先运行的窗口，这样就需要设置项目的启动窗口，其实现方法非常简单，只需要按照“运行主窗口”的步骤为要作为启动窗口的相应",{"type":19,"tag":57,"props":163,"children":165},{"className":164},[],[166],{"type":24,"value":138},{"type":24,"value":168},"文件添加程序入口即可。例如，要将",{"type":19,"tag":57,"props":170,"children":172},{"className":171},[],[173],{"type":24,"value":174},"untitled.py",{"type":24,"value":176},"（",{"type":19,"tag":57,"props":178,"children":180},{"className":179},[],[181],{"type":24,"value":182},"untitled.ui",{"type":24,"value":184},"文件对应的代码文件）作为启动窗口，则在",{"type":19,"tag":57,"props":186,"children":188},{"className":187},[],[189],{"type":24,"value":174},{"type":24,"value":191},"文件中添加如下代码：",{"type":19,"tag":193,"props":194,"children":198},"pre",{"className":195,"code":196,"language":197,"meta":7,"style":7},"language-python shiki shiki-themes github-dark","import sys  \n# 程序入口，程序从此处启动PyQt设计的窗体  \nif __name__ == '__main__':  \n   app = QtWidgets.QApplication(sys.argv)  \n   MainWindow = QtWidgets.QMainWindow() # 创建窗体对象  \n   ui = Ui_MainWindow()                 # 创建PyQt设计的窗体对象  \n   ui.setupUi(MainWindow)  # 调用PyQt窗体的方法对窗体对象进行初始化设置  \n   MainWindow.show()                    # 显示窗体  \n   sys.exit(app.exec_())                # 程序关闭时退出进程\n","python",[199],{"type":19,"tag":57,"props":200,"children":201},{"__ignoreMap":7},[202,213,222,231,240,249,258,267,276],{"type":19,"tag":203,"props":204,"children":207},"span",{"class":205,"line":206},"line",1,[208],{"type":19,"tag":203,"props":209,"children":210},{},[211],{"type":24,"value":212},"import sys  \n",{"type":19,"tag":203,"props":214,"children":216},{"class":205,"line":215},2,[217],{"type":19,"tag":203,"props":218,"children":219},{},[220],{"type":24,"value":221},"# 程序入口，程序从此处启动PyQt设计的窗体  \n",{"type":19,"tag":203,"props":223,"children":225},{"class":205,"line":224},3,[226],{"type":19,"tag":203,"props":227,"children":228},{},[229],{"type":24,"value":230},"if __name__ == '__main__':  \n",{"type":19,"tag":203,"props":232,"children":234},{"class":205,"line":233},4,[235],{"type":19,"tag":203,"props":236,"children":237},{},[238],{"type":24,"value":239},"   app = QtWidgets.QApplication(sys.argv)  \n",{"type":19,"tag":203,"props":241,"children":243},{"class":205,"line":242},5,[244],{"type":19,"tag":203,"props":245,"children":246},{},[247],{"type":24,"value":248},"   MainWindow = QtWidgets.QMainWindow() # 创建窗体对象  \n",{"type":19,"tag":203,"props":250,"children":252},{"class":205,"line":251},6,[253],{"type":19,"tag":203,"props":254,"children":255},{},[256],{"type":24,"value":257},"   ui = Ui_MainWindow()                 # 创建PyQt设计的窗体对象  \n",{"type":19,"tag":203,"props":259,"children":261},{"class":205,"line":260},7,[262],{"type":19,"tag":203,"props":263,"children":264},{},[265],{"type":24,"value":266},"   ui.setupUi(MainWindow)  # 调用PyQt窗体的方法对窗体对象进行初始化设置  \n",{"type":19,"tag":203,"props":268,"children":270},{"class":205,"line":269},8,[271],{"type":19,"tag":203,"props":272,"children":273},{},[274],{"type":24,"value":275},"   MainWindow.show()                    # 显示窗体  \n",{"type":19,"tag":203,"props":277,"children":279},{"class":205,"line":278},9,[280],{"type":19,"tag":203,"props":281,"children":282},{},[283],{"type":24,"value":284},"   sys.exit(app.exec_())                # 程序关闭时退出进程\n",{"type":19,"tag":27,"props":286,"children":288},{"id":287},"_3窗口之间的关联",[289],{"type":24,"value":290},"3  窗口之间的关联",{"type":19,"tag":34,"props":292,"children":293},{},[],{"type":19,"tag":20,"props":295,"children":296},{},[297],{"type":24,"value":298},"在多窗口创建完成后，需要将各个窗口进行关联，然后才可以形成一个完整的项目。这里以在启动窗口中打开另外3个窗口为例进行讲解。",{"type":19,"tag":20,"props":300,"children":301},{},[302,304,310,312,318,320,326,328,334],{"type":24,"value":303},"首先看一下",{"type":19,"tag":57,"props":305,"children":307},{"className":306},[],[308],{"type":24,"value":309},"untitled2.py",{"type":24,"value":311},"文件、",{"type":19,"tag":57,"props":313,"children":315},{"className":314},[],[316],{"type":24,"value":317},"untitled3.py",{"type":24,"value":319},"文件和",{"type":19,"tag":57,"props":321,"children":323},{"className":322},[],[324],{"type":24,"value":325},"untitled4.py",{"type":24,"value":327},"文件，在自动转换后的代码中，默认继承自",{"type":19,"tag":57,"props":329,"children":331},{"className":330},[],[332],{"type":24,"value":333},"object",{"type":24,"value":335},"类，代码如下：",{"type":19,"tag":193,"props":337,"children":339},{"className":195,"code":338,"language":197,"meta":7,"style":7},"class Ui_MainWindow(object):\n",[340],{"type":19,"tag":57,"props":341,"children":342},{"__ignoreMap":7},[343],{"type":19,"tag":203,"props":344,"children":345},{"class":205,"line":206},[346],{"type":19,"tag":203,"props":347,"children":348},{},[349],{"type":24,"value":338},{"type":19,"tag":20,"props":351,"children":352},{},[353,355,360,362,368,370,375,377,383],{"type":24,"value":354},"为了执行窗口操作，需要将继承的",{"type":19,"tag":57,"props":356,"children":358},{"className":357},[],[359],{"type":24,"value":333},{"type":24,"value":361},"类修改为",{"type":19,"tag":57,"props":363,"children":365},{"className":364},[],[366],{"type":24,"value":367},"QMainWindow",{"type":24,"value":369},"类，由于",{"type":19,"tag":57,"props":371,"children":373},{"className":372},[],[374],{"type":24,"value":367},{"type":24,"value":376},"类位于",{"type":19,"tag":57,"props":378,"children":380},{"className":379},[],[381],{"type":24,"value":382},"PyQt5.QtWidgets",{"type":24,"value":384},"模块中，因此需要进行导入，修改后的代码如下：",{"type":19,"tag":193,"props":386,"children":388},{"className":195,"code":387,"language":197,"meta":7,"style":7},"from PyQt5.QtWidgets import QMainWindow  \nclass Ui_MainWindow(QMainWindow):\n",[389],{"type":19,"tag":57,"props":390,"children":391},{"__ignoreMap":7},[392,400],{"type":19,"tag":203,"props":393,"children":394},{"class":205,"line":206},[395],{"type":19,"tag":203,"props":396,"children":397},{},[398],{"type":24,"value":399},"from PyQt5.QtWidgets import QMainWindow  \n",{"type":19,"tag":203,"props":401,"children":402},{"class":205,"line":215},[403],{"type":19,"tag":203,"props":404,"children":405},{},[406],{"type":24,"value":407},"class Ui_MainWindow(QMainWindow):\n",{"type":19,"tag":20,"props":409,"children":410},{},[411,413,418,419,424,425,430,432,437,439,444,446,452],{"type":24,"value":412},"修改完",{"type":19,"tag":57,"props":414,"children":416},{"className":415},[],[417],{"type":24,"value":309},{"type":24,"value":311},{"type":19,"tag":57,"props":420,"children":422},{"className":421},[],[423],{"type":24,"value":317},{"type":24,"value":319},{"type":19,"tag":57,"props":426,"children":428},{"className":427},[],[429],{"type":24,"value":325},{"type":24,"value":431},"文件的继承类之后，打开",{"type":19,"tag":57,"props":433,"children":435},{"className":434},[],[436],{"type":24,"value":174},{"type":24,"value":438},"主窗口文件，在该文件中，首先定义一个槽函数，用来使用",{"type":19,"tag":57,"props":440,"children":442},{"className":441},[],[443],{"type":24,"value":367},{"type":24,"value":445},"对象的",{"type":19,"tag":57,"props":447,"children":449},{"className":448},[],[450],{"type":24,"value":451},"show()方法",{"type":24,"value":453},"打开3个窗口，代码如下：",{"type":19,"tag":193,"props":455,"children":457},{"className":195,"code":456,"language":197,"meta":7,"style":7},"def open(self):  \n    import untitled2, untitled3, untitled4  \n    self.second = untitled2.Ui_MainWindow() # 创建第2个窗体对象  \n    self.second.show()  # 显示窗体  \n    self.third = untitled3.Ui_MainWindow()  # 创建第3个窗体对象  \n    self.third.show()  # 显示窗体  \n    self.fouth = untitled4.Ui_MainWindow()  # 创建第4个窗体对象  \n    self.fouth.show()  # 显示窗体\n",[458],{"type":19,"tag":57,"props":459,"children":460},{"__ignoreMap":7},[461,469,477,485,493,501,509,517],{"type":19,"tag":203,"props":462,"children":463},{"class":205,"line":206},[464],{"type":19,"tag":203,"props":465,"children":466},{},[467],{"type":24,"value":468},"def open(self):  \n",{"type":19,"tag":203,"props":470,"children":471},{"class":205,"line":215},[472],{"type":19,"tag":203,"props":473,"children":474},{},[475],{"type":24,"value":476},"    import untitled2, untitled3, untitled4  \n",{"type":19,"tag":203,"props":478,"children":479},{"class":205,"line":224},[480],{"type":19,"tag":203,"props":481,"children":482},{},[483],{"type":24,"value":484},"    self.second = untitled2.Ui_MainWindow() # 创建第2个窗体对象  \n",{"type":19,"tag":203,"props":486,"children":487},{"class":205,"line":233},[488],{"type":19,"tag":203,"props":489,"children":490},{},[491],{"type":24,"value":492},"    self.second.show()  # 显示窗体  \n",{"type":19,"tag":203,"props":494,"children":495},{"class":205,"line":242},[496],{"type":19,"tag":203,"props":497,"children":498},{},[499],{"type":24,"value":500},"    self.third = untitled3.Ui_MainWindow()  # 创建第3个窗体对象  \n",{"type":19,"tag":203,"props":502,"children":503},{"class":205,"line":251},[504],{"type":19,"tag":203,"props":505,"children":506},{},[507],{"type":24,"value":508},"    self.third.show()  # 显示窗体  \n",{"type":19,"tag":203,"props":510,"children":511},{"class":205,"line":260},[512],{"type":19,"tag":203,"props":513,"children":514},{},[515],{"type":24,"value":516},"    self.fouth = untitled4.Ui_MainWindow()  # 创建第4个窗体对象  \n",{"type":19,"tag":203,"props":518,"children":519},{"class":205,"line":269},[520],{"type":19,"tag":203,"props":521,"children":522},{},[523],{"type":24,"value":524},"    self.fouth.show()  # 显示窗体\n",{"type":19,"tag":20,"props":526,"children":527},{},[528,530,536,538,544,546,552],{"type":24,"value":529},"然后将",{"type":19,"tag":57,"props":531,"children":533},{"className":532},[],[534],{"type":24,"value":535},"PushButton",{"type":24,"value":537},"按钮的",{"type":19,"tag":57,"props":539,"children":541},{"className":540},[],[542],{"type":24,"value":543},"clicked()信号",{"type":24,"value":545},"与自定义的槽函数",{"type":19,"tag":57,"props":547,"children":549},{"className":548},[],[550],{"type":24,"value":551},"open()",{"type":24,"value":553},"相关联，代码如下：",{"type":19,"tag":193,"props":555,"children":557},{"className":195,"code":556,"language":197,"meta":7,"style":7},"self.pushButton.clicked.connect(self.open)\n",[558],{"type":19,"tag":57,"props":559,"children":560},{"__ignoreMap":7},[561],{"type":19,"tag":203,"props":562,"children":563},{"class":205,"line":206},[564],{"type":19,"tag":203,"props":565,"children":566},{},[567],{"type":24,"value":556},{"type":19,"tag":20,"props":569,"children":570},{},[571,573,578,580,586],{"type":24,"value":572},"运行",{"type":19,"tag":57,"props":574,"children":576},{"className":575},[],[577],{"type":24,"value":174},{"type":24,"value":579},"主窗口，单击",{"type":19,"tag":57,"props":581,"children":583},{"className":582},[],[584],{"type":24,"value":585},"打开",{"type":24,"value":587},"按钮，即可打开其他3个窗口，效果如图所示。",{"type":19,"tag":20,"props":589,"children":590},{},[591],{"type":19,"tag":93,"props":592,"children":595},{"alt":593,"src":594},"Pasted image 20250827152909","\u002Fimages\u002Fblog\u002Fpyqt5\u002FPasted%20image%2020250827152909.png",[],{"type":19,"tag":597,"props":598,"children":599},"style",{},[600],{"type":24,"value":601},"html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}",{"title":7,"searchDepth":215,"depth":215,"links":603},[604,605,606],{"id":29,"depth":215,"text":32},{"id":150,"depth":215,"text":153},{"id":287,"depth":215,"text":290},"markdown","content:blog:2025-08-21-pyqt5-多窗口设计.md","content","blog\u002F2025-08-21-pyqt5-多窗口设计.md","blog\u002F2025-08-21-pyqt5-多窗口设计","md",1780801018173]