如何选择在Python中运行的类?

2024-10-01 07:15:59 发布

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

我在python3.3中使用PyQt4,制作了一个GUI并有多个类,其中一些类在单击某个按钮之前不想运行。如何连接这样一个类,使其仅在单击按钮时运行,而不在程序启动时运行。 下面是我当前如何将这个类连接到另一个类中的按钮。你知道吗

btns.clicked.connect(self.tableshow2)   
def tableshow2(self):
        table5.show()

这是第一个有按钮的班级。你知道吗

class CustTableSearch(QtGui.QDialog):
    def __init__(self, parent=None):
        super(CustTableSearch, self).__init__(parent)
        with sqlite3.connect('database.db') as db:
            cursor=db.cursor()
            num = QtGui.QInputDialog.getText(self, 'Insert TelephoneNumber', 
            'Enter TeleNum:')

table5 = CustTableSearch()

这是按钮激活的类的一部分,在pythonshell启动时运行。我试着用按钮把它放到类中的函数中,但是我不能用.show()显示它(这是一个带表的屏幕)。你知道吗


Tags: selfdbinitdefshowconnectgui按钮
2条回答

一种方法是只按需创建对话框,而不是在加载模块时立即创建对话框。你知道吗

class ProfilePage(QtGui.QMainWindow):
    def __init__(self):
        super(ProfilePage, self).__init__()
        self.table5 = None
        self.initUI()

    def initUI(self):
        ...
        btns.clicked.connect(self.tableshow2)   

    def tableshow2(self):
        if self.table5 is None:
            self.table5 = CustomTableSearch()
        self.table5.show()

假设两个类在同一个模块中,您可以在tableshow2(self)方法中创建CustomTableSearch的实例。你知道吗

...
def tableshow2(self):
    self.table5 = CustomTableSearch(self)
    self.table5.show()
...

相关问题 更多 >