簡體   English   中英

如何選擇在Python中運行的類?

[英]How can I choose which classes run in Python?

我在Python 3.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()

這是按鈕激活的類的一部分,該類在python shell啟動時運行。 我試着用按鈕將其放在類中的函數中,但是然后我無法使用.show()來顯示它(它是帶有表的屏幕)。

假設兩個類都在同一個模塊中,則可以在tableshow2(self)方法中創建CustomTableSearch的實例。

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

一種方法是僅按需創建對話框,而不是在加載模塊后立即創建對話框。

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()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM