Pyqt4,无法在python中打开新窗口

时间:2016-01-26 02:57:55

标签: python python-2.7 python-3.x pyqt pyqt4

我是python的绿色。我想在这里创建一个应用程序是我的所有代码。如果数据匹配成功,应用程序将工作,然后它将显示另一个窗口。

from PyQt4.QtCore import SIGNAL
from PyQt4.QtGui import QDialog, QApplication, QPushButton, QLineEdit, QFormLayout
from PyQt4 import QtCore, QtGui
import sys
import os
import MySQLdb as mdb


class Form(QDialog):
    def __init__(self, parent=None):
        super(Form, self).__init__(parent)

        self.le = QLineEdit()
        self.le.setObjectName("host")
        self.le.setText("Host")

        self.pb = QPushButton()
        self.pb.setObjectName("connect")
        self.pb.setText("Connect") 

        layout = QFormLayout()
        layout.addWidget(self.le)
        layout.addWidget(self.pb)

        self.setLayout(layout)
        self.connect(self.pb, SIGNAL("clicked()"),self.button_click)
        self.setWindowTitle("Learning")

    def button_click(self):
        # shost is a QString object
        shost = self.le.text()
        if shost:

           s_h = "127.0.0.1"
           s_n = "root"
           s_p = ""
           s_d = "code"

           s_cn = mdb.connect(s_h, s_n, s_p, s_d)

           cursor = s_cn.cursor()

           query = "SELECT * FROM  `ac` WHERE  `acc` = %s "
           re = cursor.execute(query,(shost,))

           if re:

               ex = Example()
               ex.show()                

           else:
              print "no"




class Example(QtGui.QWidget):

    def __init__(self):
        super(Example, self).__init__()

        self.initUI()


    def run(self):
        os.startfile("hyt.exe")
    def initUI(self):

        QtGui.QToolTip.setFont(QtGui.QFont('SansSerif', 10))

        self.setToolTip('This is a <b>QWidget</b> widget')

        btn1 = QtGui.QPushButton('Start', self)


        btn1.resize(btn1.sizeHint())
        btn1.move(20, 20)

        btn1.clicked.connect(self.run)

        qbtn = QtGui.QPushButton('Quit', self)
        qbtn.clicked.connect(QtCore.QCoreApplication.instance().quit)
        qbtn.resize(qbtn.sizeHint())
        qbtn.move(150, 20)

        self.setGeometry(300, 300, 250, 150)
        self.setWindowTitle('Python Script')



app = QApplication(sys.argv)
form = Form()
form.show()
app.exec_()

但我的问题是,当我按下按钮然后它将显示Example()窗口。但是在这里,当我按下按钮然后它显示窗口1秒钟,所以我该如何解决它?

 if re:

               ex = Example()
               ex.show()                

           else:
              print "no"

0 个答案:

没有答案