如何在主窗口中打开一个按钮打开一个不同的窗口?

时间:2012-06-20 05:21:12

标签: python qt pyside

我对此非常陌生,并使用Qt Designer制作了一个非常简单的主窗口ui。我想在项目中使用的第一个功能是单击按钮打开不同的窗口。

所以基本上我有使用pyside-uic生成的文件autoGenUI.py,包括

from PySide import QtCore, QtGui

class AutoGeneratedUI(object): 
    def setupUi(self, MainWindow):
        #Auto generated code

    def retranslateUi(self, MainWindow):
        #Auto generated code

这一切都很好,当然因为Qt设计师做到了。然后我有自己的.py文件,基本上是我的应用程序。

看起来像这样:

import sys

from PySide.QtCore import *
from PySide.QtGui import *

from autoGenUI import *

class MyMainWindow(QtGui.QMainWindow):
    def __init__(self, parent=None):
        super(MyMainWindow, self).__init__(parent)
        self.ui = AutoGeneratedUI()
        self.ui.setupUi(self)

if __name__ == "__main__":
    app = QApplication(sys.argv)
    myMainWindow = MyMainWindow()
    myMainWindow.show()
    sys.exit(app.exec_())

我的按钮在自动生成的python UI中被称为self.pushButton。我想设计另一个窗口,然后调用那个窗口,但现在一切都会做。我只是不知道在哪里放置代码让我的按钮做点什么。

我试图关注docs但无法正常工作。

非常感谢任何帮助。

由于

1 个答案:

答案 0 :(得分:1)

您需要将pushButton的点击信号连接到on_button_clicked()这样的方法:

def __init__(self, parent=None):
    super(MyMainWindow, self).__init__(parent)
    self.ui = AutoGeneratedUI()
    self.ui.setupUi(self)
    # connect the clicked signal to on_button_clicked() method
    self.pushButton.clicked.connect(self.on_button_clicked) 

def on_button_clicked(self):
    print "button clicked"
    # here is the code to open a new window