主对话框是如何工作的

2024-05-04 21:36:57 发布

您现在位置:Python中文网/ 问答频道 /正文

我在试着理解我的生命周期主.py“工作。它基于网上的例子,关于PySide和Qt设计器,来实现pythongui。你知道吗

代码是:

#***********************************#
# Python Libraries                  #
#***********************************#
from PySide.QtCore import *
from PySide.QtGui import *
import sys
import time
#***********************************#
# Python files                      #
#***********************************#
import Gui
from server import *

class MainDialog(QDialog, Gui.Ui_TCPServer):

    def __init__(self, parent=None):

        super(MainDialog, self).__init__(parent)
        self.setupUi(self)

        self.connect(self.ConnectBt, SIGNAL("clicked()"), self.ConnectBt_clicked)
        self.connect(self.QuitBt, SIGNAL("clicked()"), self.QuitBt_clicked)
        self.connect(self.DisconnectBt, SIGNAL("clicked()"), self.DisconnectBt_clicked)

        print "NOW HERE\r\n"

    def ConnectBt_clicked(self):
        self.ConnectBt.setText("Connecting...")
        self.server_connect()
        print "THEN HERE\r\n"

    def QuitBt_clicked(self):
        self.close()

    def DisconnectBt_clicked(self):
        self.ConnectBt.setText("Connect")
        self.server_off = ChronoRequestHandler()
        self.server_off.finish()

    def server_connect(self):
        self.server_on = ServerStart()
        self.server_on.try_connect()

if __name__ == '__main__':
    app = QApplication(sys.argv)
    form = MainDialog()
    print "HERE\r\n"
    form.show()
    app.exec_()
    print "END\r\n"

当我打电话给主.py我得到一张“现在在这里”和“然后在这里”的照片。当我按下“ConnectBt”键时,我得到的是“THEN HERE”的字样。你知道吗

但在这之后,这个循环又会在哪里呢?它是否返回到init,如果是,我是否应该再次打印“NOW HERE”?它是否返回到main,如果是,我不应该打印“HERE”吗?请给我解释一下。。。你知道吗

当我按下“退出”键时,我得到的是“结束”。。。我糊涂了!你知道吗

谢谢。你知道吗


Tags: fromimportselfsignalhereserverinitdef
1条回答
网友
1楼 · 发布于 2024-05-04 21:36:57

我认为您应该更清楚地了解对象编程和事件是如何工作的。你知道吗

在最后一个if语句(底部的代码,当您从终端调用脚本时运行)中,您创建了QApplication的app object实例。你知道吗

然后创建窗体,MainDialog的实例,MainDialog是您在上面定义的类(从两个类继承方法、属性等,QDialog,Gui.Ui\TCPServer). 你知道吗

通过做

form = MainDialog()

你运行init,打印“NOW HERE”然后退出这个方法。请检查\uu init \uuuuuuuuuuuu在Python中的作用。why-do-we-use-init-in-python-classes

在结束之前,调用app实例的exec()方法。它包含一个循环,以便您的接口收集和处理事件。请参阅上的文档QApplication.exec()如下。你知道吗

当您按下“ConnectBt”按钮时,您将调用ConnectBt\u clicked()方法,该方法进行填充(与服务器连接)并打印“THEN HERE”。你知道吗

同样,当您按QuitBt时,调用QuitBt\u clicked(),这将关闭连接并让代码打印“END”。你知道吗

我还建议您阅读更多关于您正在使用的类的文档。他们将解释为什么不同的按钮是“链接的”/回调方法ConnectBt\u clicked()、def QuitBt\u clicked()和DisconnectBt\u clicked()。按钮触发这些回调的机制在这些类中实现的代码中是隐式的。你知道吗

QApplication Class Reference: exec_

int QApplication.exec_ ()

Enters the main event loop and waits until exit() is called, then returns the value that was set to exit() (which is 0 if exit() is called via quit()).

It is necessary to call this function to start event handling. The main event loop receives events from the window system and dispatches these to the application widgets.

Generally, no user interaction can take place before calling exec(). As a special case, modal widgets like QMessageBox can be used before calling exec(), because modal widgets call exec() to start a local event loop.

To make your application perform idle processing, i.e., executing a special function whenever there are no pending events, use a QTimer with 0 timeout. More advanced idle processing schemes can be achieved using processEvents().

We recommend that you connect clean-up code to the aboutToQuit() signal, instead of putting it in your application's main() function. This is because, on some platforms the QApplication.exec() call may not return. For example, on the Windows platform, when the user logs off, the system terminates the process after Qt closes all top-level windows. Hence, there is no guarantee that the application will have time to exit its event loop and execute code at the end of the main() function, after the QApplication.exec() call.

See also quitOnLastWindowClosed, quit(), exit(), processEvents(), and QCoreApplication.exec().

相关问题 更多 >