__init __()中的PyQt self.close() [英] PyQt self.close() in __init__()

查看:216
本文介绍了__init __()中的PyQt self.close()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Python 2.7下使用PyQt4时遇到一些小问题

I encountered a few minor issues while working with PyQt4 under Python 2.7

我正在写一个小项目,其中一些QDialogs相互打开. 因此,我打开了一个对话框,并立即打开了另一个对话框来检查某些内容,当出现错误检查时,我希望整个对话框都关闭.看起来像这样:

I'm writing a little project where there are some QDialogs opening each other. So there is one Dialog I open and instantly open another Dialog to check something and when there is an error checking, i want the whole dialog to close. it looks like this:

class MyDialog(QtGui.QDialog):
    def __init__(self):
        ## should get me a 10 digit number input
        text, ok = QtGui.QInputDialog.getText(self, u'Enter check')
    if ok:
        ## if clicked ok, assign tID
        tID = text
    else:
        ## else close
        self.close()

    try:   
        ## checker is a plausibilty check for the input
        ## checks, if tID is a number and 10 digits long
        ## and throws an IntegrityWarning if one of these conditions
        ## is not met         
        tID = self.checker.checkID(tID)
    except IntegrityWarning as e:
        ## on exception show error dialog
        errDialog = QtGui.QMessageBox()
        errDialog.setWindowTitle(u'Fehler')
        errDialog.setText(e.getWarning())
        ok = errDialog.exec_()
        if ok != True:
            ## close the whole dialog if user cancels
            self.close()

    self.tAccount = self.tControl.getAccount(tID)

所以这基本上是我要做的,并且工作正常,只是我在分配之前得到了使用tID的错误消息.之后,我的程序可以正常工作,但是我想知道错误是从哪里来的.令我感到奇怪的是,即使我将tAccount的最后一次分配放入try-except:pass语句中,该错误也会被抛出到控制台,而不是通过,这显然是事实.所以我的猜测是我不能简单地在__init__()例程中终止该程序.我尝试了一个return语句而不是close()语句,并销毁了MainWindow调用程序函数中的QDialog.这使得该对话框出现并保持空白,因为它是模式对话框,我不得不关闭它才能在MainWindow中恢复.

So this is basically what I do and it works just fine except that I get the Errormessage of using tID before assignment. My Programm works just fine after that but I wonder where the error comes from. What strikes me as curious though, is the fact that even if i put the last assignment of tAccount into a try-except: pass statement, the error is thrown to the console, and not passed, as it should be obviously. So my guess was that i can't simply terminate the programm in the __init__() routine. I tried a return statement instead of the close() statement and destroyed the QDialog in the caller function which is in the MainWindow. This made the Dialog appear and stay empty, as it as modal, i had to close it to resume in the MainWindow.

有人可以告诉我为什么对话框__init__()在关闭后恢复吗?

Can someone please tell me why the Dialog __init__() resumes after being closed?

推荐答案

这是Python的正常行为. self.close()不是return语句,因此它不会从__init__退出.

This is normal behavior of Python. self.close() is not a return statement, so it will not exit from __init__.

此外,__init__实际上不会在屏幕上显示对话框,因此在__init__中写入self.close()是没有意义的.我建议您对代码进行重组,以便此应用程序逻辑不在__init__的范围内,并将根据QInputDialogchecker

Furthermore, __init__ does not actually show the dialog on screen, so writing self.close() in __init__ is pointless. I'd suggest restructuring your code so this application logic is outside of the __init__ and will decide whether to initialize and show a MyDialog based on the results from QInputDialog and checker

这篇关于__init __()中的PyQt self.close()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆