我正在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()
...
关于python - 如何选择在 Python 中运行的类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22286646/