使用QPushButton连接多个QT窗口

时间:2016-03-29 14:54:30

标签: python qt pyside

我正在开发一个项目,我想为此开发GUI。 我用Qt Designer创建了4个不同的UI文件,并用PySide(pyside-uic)将它们转换成python文件。 现在,我想创建一个主python文件来连接这些窗口。 我的软件结构如下:

1 - 欢迎窗口:按下按钮加载主窗口并关闭欢迎窗口 2 - 主窗口:它有2个按钮可以打开我的第三个和第四个窗口。

我已阅读this postthis one帖子,但我无法完成(我是新手)。

这是我欢迎窗口的代码:

<nav id="menuWorkshops">
  <header>
    <dl>
      <dt>Facile</dt>
      <dd class="circle green"></dd>
    </dl>
    <dl>
      <dt>Moyen</dt>
      <dd class="circle orange"></dd>
    </dl>
    <dl>
      <dt>Difficile</dt>
      <dd class="circle red"></dd>
    </dl>
  </header>
  <ul>
    <li>Hardware
      <ul class="submenu">
        <li>
          <a href="#">
            <img alt="" src="img/hardware_web.png" />Montage PC
            <span class="circle orange"></span>
          </a>
        </li>
      </ul>
    </li>
    <li>Système
      <ul class="submenu">
        <li>
          <a href="#">
            <img alt="" src="img/os_web.png" />Installation OS
            <span class="circle green"></span>
          </a>
        </li>
      </ul>
    </li>
    <li>Programmation
      <ul class="submenu">
        <li>
          <a href="#">
            <img alt="" src="img/html_web.png" />Développement Web
            <span class="circle orange"></span>
          </a>
        </li>
        <li>
          <a href="#">
            <img alt="" src="img/lego_web.png" />Lego Mindstorm
            <span class="circle green"></span>
          </a>
        </li>
        <li>
          <a href="#">
            <img alt="" src="img/catch_me_web.png" />Jeu Attrape-moi (Processing)
            <span class="circle red"></span>
          </a>
        </li>
        <li>
          <a href="#">
            <img alt="" src="img/tetris_web.png" />Jeu Tetris (Small Basic)
            <span class="circle red"></span>
          </a>
        </li>
        <li>
          <a href="#">
            <img alt="" src="img/breakout_web.png" />Jeu Casse-Brique (Small Basic)
            <span class="circle orange"></span>
          </a>
        </li>
        <li>
          <a href="#">
            <img alt="" src="img/scratch_web.png" />Kinect to Scratch
            <span class="circle green"></span>
          </a>
        </li>
        <li>
          <a href="#">
            <img alt="" src="img/bird_web.png" />Jeu Flappy Bird (Scratch)
            <span class="circle green"></span>
          </a>
        </li>
      </ul>
    </li>
  </ul>
</nav>

这是我的主窗口:

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'welcome_window_gui.ui'
#
# Created: Tue Mar 29 16:49:21 2016
#      by: pyside-uic 0.2.15 running on PySide 1.2.4

#
# WARNING! All changes made in this file will be lost!

from PySide import QtCore, QtGui

class Ui_welcome_window(object):
    def setupUi(self, welcome_window):
        welcome_window.setObjectName("welcome_window")
        welcome_window.resize(474, 300)
        self.verticalLayout_2 = QtGui.QVBoxLayout(welcome_window)
        self.verticalLayout_2.setObjectName("verticalLayout_2")
        self.welcomeLBL = QtGui.QLabel(welcome_window)
        self.welcomeLBL.setAlignment(QtCore.Qt.AlignCenter)
        self.welcomeLBL.setObjectName("welcomeLBL")
        self.verticalLayout_2.addWidget(self.welcomeLBL)
        self.welcomeBTN = QtGui.QPushButton(welcome_window)
        self.welcomeBTN.setObjectName("welcomeBTN")
        self.verticalLayout_2.addWidget(self.welcomeBTN)

        self.retranslateUi(welcome_window)
        QtCore.QObject.connect(self.welcomeBTN, QtCore.SIGNAL("clicked()"), welcome_window.close)
        QtCore.QMetaObject.connectSlotsByName(welcome_window)

    def retranslateUi(self, welcome_window):
        welcome_window.setWindowTitle(QtGui.QApplication.translate("welcome_window", "Welcome", None, QtGui.QApplication.UnicodeUTF8))
        self.welcomeLBL.setText(QtGui.QApplication.translate("welcome_window", "Welcome TO the Software", None, QtGui.QApplication.UnicodeUTF8))
        self.welcomeBTN.setText(QtGui.QApplication.translate("welcome_window", "Enter to Software", None, QtGui.QApplication.UnicodeUTF8))


if __name__ == "__main__":
    import sys
    app = QtGui.QApplication(sys.argv)
    welcome_window = QtGui.QWidget()
    ui = Ui_welcome_window()
    ui.setupUi(welcome_window)
    welcome_window.show()
    sys.exit(app.exec_())

有没有办法在主Python代码中调用这些类?

感谢。

1 个答案:

答案 0 :(得分:1)

您只需在主文件中导入它们即可:

from first_file import Ui_welcome_window
from second_file import Ui_main_window

然后你可以像往常一样从那里使用它们 (注意,未执行2个文件中的if __name__ == "__main__"部分)

制作按钮:

#create the 2 windows
main_window = QtGui.QWidget()
ui = Ui_main_window()
ui.setupUi(main_window)

welcome_window = QtGui.QWidget()
ui2 = Ui_welcome_window()
ui2.setupUi(welcome_window)

ui2.welcomeBTN.clicked.connect(main_window.show)

当您单击welcome_window上的Button时,main_window应为show()。对第3和第4窗口做同样的事情!

问候,MrP01