• 3520阅读
  • 0回复

PYQT4 多窗口切换问题 [复制链接]

上一主题 下一主题
离线lason123
 

只看楼主 倒序阅读 楼主  发表于: 2017-08-14
关键词: PYQT4,
hi,坛里的高手们
引用一片文章里的例子
http://blog.csdn.net/sollor525/article/details/40076395
在这个例子中,用PYQT4 实现了多窗口的切换,但是有一个问题想请教下,主窗体在调用dialog1和dialog2的时候,被调用的窗体dialog1和dialog2上的工具栏上原本是缩小和放大的图案显示的是问号,我想这个应该是调用QDailog类窗体的原因吧,那么如果要把这个改成QWidget类型的话,是不是就ok了,但是我不知道该怎么写,请大牛帮助下,谢谢!
window.open('http://www.qtcn.org/bbs/attachment/Mon_1708/106_180939_9e53ed1488735e9.png?76');" style="max-width:700px;max-height:700px;" onload="if(is_ie6&&this.offsetWidth>700)this.width=700;" >

界面函数:
# -*- coding: utf-8 -*-

from PyQt4 import QtCore, QtGui
from dialog1 import Dialog1
from dialog2 import Dialog2
import sys

try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    def _fromUtf8(s):
        return s

try:
    _encoding = QtGui.QApplication.UnicodeUTF8
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig)
    
class MainWindow(QtGui.QWidget):
    
    dialog1_signal = QtCore.pyqtSignal()          #定义一个无参数的信号,串口设定与子站初始化信号
    dialog2_signal = QtCore.pyqtSignal()          #定义一个无参数的信号,串口设定与子站初始化信号
    exit_signal = QtCore.pyqtSignal()          #定义一个无参数的信号,串口设定与子站初始化信号
    
    def __init__(self):
        super(MainWindow,self).__init__()
        
    def setupUi(self, Form):
        Form.setObjectName(_fromUtf8("Form"))
        Form.resize(400, 300)
        self.form = Form
        self.pushButton = QtGui.QPushButton(Form)
        self.pushButton.setGeometry(QtCore.QRect(70, 90, 75, 23))
        self.pushButton.setObjectName(_fromUtf8("pushButton"))
        self.pushButton_2 = QtGui.QPushButton(Form)
        self.pushButton_2.setGeometry(QtCore.QRect(240, 90, 75, 23))
        self.pushButton_2.setObjectName(_fromUtf8("pushButton_2"))
        self.pushButton_3 = QtGui.QPushButton(Form)
        self.pushButton_3.setGeometry(QtCore.QRect(150, 160, 75, 23))
        self.pushButton_3.setObjectName(_fromUtf8("pushButton_3"))
        self.label = QtGui.QLabel(Form)
        self.label.setGeometry(QtCore.QRect(170, 40, 54, 16))
        self.label.setObjectName(_fromUtf8("label"))

        self.retranslateUi(Form)
        QtCore.QMetaObject.connectSlotsByName(Form)

        #信号连接到指定槽
        self.pushButton.clicked.connect(self.on_pushButton_clicked)
        self.pushButton_2.clicked.connect(self.on_pushButton_2_clicked)
        self.pushButton_3.clicked.connect(self.on_pushButton_3_clicked)
        
        
    def retranslateUi(self, Form):
        Form.setWindowTitle(_translate("Form", "Form", None))
        self.pushButton.setText(_translate("Form", "进入dialog1", None))
        self.pushButton_2.setText(_translate("Form", "进入dialog2", None))
        self.pushButton_3.setText(_translate("Form", "退出", None))
        self.label.setText(_translate("Form", "主窗体", None))
        
    def on_pushButton_clicked(self):
        self.form.hide()
        Form1 = QtGui.QDialog()
        ui = Dialog1()
        ui.setupUi(Form1)
        Form1.show()
        Form1.exec_()
        self.form.show()

    def on_pushButton_3_clicked(self, Form):
        #QtCore.QObject.connect( self.pushButton_3, QtCore.SIGNAL("clicked()"), self, QtCore.SLOT(quit()))
        #也可以这样
        self.form.close()
        
    def on_pushButton_2_clicked(self):
        self.form.close()
        Form1 = QtGui.QDialog()
        ui = Dialog2()
        ui.setupUi(Form1)
        Form1.show()
        Form1.exec_()
        self.form.show()

if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    Form = QtGui.QWidget()
    window = MainWindow()  
    window.setupUi(Form)
    Form.show()  
    sys.exit(app.exec_())

dialog1界面:
# -*- coding: utf-8 -*-

from PyQt4 import QtCore, QtGui


try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    def _fromUtf8(s):
        return s

try:
    _encoding = QtGui.QApplication.UnicodeUTF8
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig)

class Dialog1(QtGui.QWidget):
    def setupUi(self, Dialog):
        Dialog.setObjectName(_fromUtf8("Dialog"))
        Dialog.resize(400, 300)
        self.form = Dialog
        self.label = QtGui.QLabel(Dialog)
        self.label.setGeometry(QtCore.QRect(180, 50, 54, 12))
        self.label.setObjectName(_fromUtf8("label"))
        self.dialog1_pushButton = QtGui.QPushButton(Dialog)
        self.dialog1_pushButton.setGeometry(QtCore.QRect(160, 130, 75, 23))
        self.dialog1_pushButton.setObjectName(_fromUtf8("pushButton"))

        self.retranslateUi(Dialog)
        QtCore.QMetaObject.connectSlotsByName(Dialog)

        #信号连接到指定槽
        self.dialog1_pushButton.clicked.connect(self.on_dialog1_pushButton_clicked)
        
    def retranslateUi(self, Dialog):
        Dialog.setWindowTitle(_translate("Dialog", "Dialog", None))
        self.label.setText(_translate("Dialog", "dialog1", None))
        self.dialog1_pushButton.setText(_translate("Dialog", "返回主窗体", None))

    def on_dialog1_pushButton_clicked(self):
        self.form.close()

if __name__ == "__main__":
    import sys
    app = QtGui.QApplication(sys.argv)
    Dialog = QtGui.QDialog()
    ui = Dialog1()
    ui.setupUi(Dialog)
    Dialog.show()
    sys.exit(app.exec_())


dialog2界面:
# -*- coding: utf-8 -*-

from PyQt4 import QtCore, QtGui


try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    def _fromUtf8(s):
        return s

try:
    _encoding = QtGui.QApplication.UnicodeUTF8
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig)

class Dialog2(object):
    def setupUi(self, Dialog):
        Dialog.setObjectName(_fromUtf8("Dialog"))
        Dialog.resize(400, 300)
        self.form = Dialog
        self.label = QtGui.QLabel(Dialog)
        self.label.setGeometry(QtCore.QRect(180, 60, 54, 12))
        self.label.setObjectName(_fromUtf8("label"))
        self.pushButton = QtGui.QPushButton(Dialog)
        self.pushButton.setGeometry(QtCore.QRect(160, 140, 75, 23))
        self.pushButton.setObjectName(_fromUtf8("pushButton"))

        self.retranslateUi(Dialog)
        QtCore.QMetaObject.connectSlotsByName(Dialog)
        
        #信号连接到指定槽
        self.pushButton.clicked.connect(self.on_pushButton_clicked)
        
    def retranslateUi(self, Dialog):
        Dialog.setWindowTitle(_translate("Dialog", "Dialog", None))
        self.label.setText(_translate("Dialog", "dialog2", None))
        self.pushButton.setText(_translate("Dialog", "返回主窗体", None))
        
    def on_pushButton_clicked(self):
        self.form .close()

if __name__ == "__main__":
    import sys
    app = QtGui.QApplication(sys.argv)
    Dialog = QtGui.QDialog()
    ui = Dialog2()
    ui.setupUi(Dialog)
    Dialog.show()
    sys.exit(app.exec_())

其实,如果将Dialog1和Dialog2中的主函数中Dialog = QtGui.QDialog()改成Dialog = QtGui.QWidget(),单独运行后工具栏 上 都不会有问号出现,但是当它们都被主界面函数调用的时候,如果在主界面函数中写成
    def on_pushButton_clicked(self):
        self.form.hide()
        Form1 = QtGui.QWidget ()
        ui = Dialog1()
        ui.setupUi(Form1)
        Form1.show()
        Form1.exec_()
        self.form.show()

就会报错
AttributeError: 'QWidget' object has no attribute 'exec_'

那么我应该怎么写,才能实现两个被调用窗体显示QWidget该有的样式,就是工具栏不显示问号
快速回复
限100 字节
 
上一个 下一个