PyQT 动态更改 QMainWindow 标题
PyQT change dynamically a QMainWindow title
我制作了一个非常简单的 python 脚本来将 sqlite3 数据库读入 QtTableWidget。
import sqlite3 as db
from PyQt4 import QtCore, QtGui
import sys
con = db.connect('results.db', isolation_level=None)
cur = con.cursor()
cur.execute("SELECT * FROM Results")
all_data = cur.fetchall()
class UiDialog(object):
def setupUi(self, datadb):
datadb.setObjectName("Dialog")
datadb.resize(404, 304)
datadb.setWindowTitle("Database results")
sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Fixed, QtGui.QSizePolicy.Fixed)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(datadb.sizePolicy().hasHeightForWidth())
datadb.setSizePolicy(sizePolicy)
datadb.setMinimumSize(QtCore.QSize(404, 304))
datadb.setMaximumSize(QtCore.QSize(404, 304))
self.table = QtGui.QTableWidget(datadb)
self.table.setGeometry(QtCore.QRect(2, 2, 400, 261))
self.table.setObjectName("table")
self.show = QtGui.QPushButton(datadb)
self.show.setGeometry(QtCore.QRect(2, 270, 400, 31))
self.show.setObjectName("show")
self.show.setText("Show results")
QtCore.QObject.connect(self.show, QtCore.SIGNAL("clicked()"), self.populate)
QtCore.QMetaObject.connectSlotsByName(datadb)
def populate(self):
self.table.setRowCount(len(all_data))
self.table.setColumnCount(4)
self.table.setHorizontalHeaderLabels(['Number', 'Keys', 'Time', 'Tries'])
for i, item in enumerate(all_data):
number = QtGui.QTableWidgetItem(str(item[0]))
keys = QtGui.QTableWidgetItem(item[1])
time = QtGui.QTableWidgetItem(str(item[2]))
tries = QtGui.QTableWidgetItem(str(item[3]))
self.table.setItem(i, 0, number)
self.table.setItem(i, 1, keys)
self.table.setItem(i, 2, time)
self.table.setItem(i, 3, tries)
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
main_conf = QtGui.QDialog()
ui = UiDialog()
ui.setupUi(main_conf)
main_conf.show()
ret = app.exec_()
sys.exit(ret)
它工作正常。我现在的问题是:
有什么方法可以在 运行 时间动态更改 window 标题吗?
setWindowTitle() 不会即时更新标题栏,update() 或 repaint() 也不会。
非常感谢您抽出宝贵的时间!
def setupUi(self, datadb):
[...]
self.datadb = datadb
def populate(self):
[...]
self.datadb.setWindowTitle('new title')
这行得通。但是您正在以一种奇怪且笨拙的方式构建您的小部件。比较常规的是使用继承:
class DatabaseUi(QtGui.QDialog):
def __init__(self):
QtGui.QDialog.__init__(self)
self.table = QtGui.QTableWidget()
self.button = QtGui.QPushButton()
self.button.setText("Show results")
self.button.clicked.connect(self.populate)
vlay = QtGui.QVBoxLayout()
vlay.addWidget(self.table)
vlay.addWidget(self.button)
self.setLayout(vlay)
self.setFixedSize(404, 304)
self.setWindowTitle("Database results")
def populate(self):
self.setWindowTitle('new title')
self.table.setColumnCount(4)
self.table.setHorizontalHeaderLabels(['Number', 'Keys', 'Time', 'Tries'])
[...]
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
ui = DatabaseUi()
ui.show()
sys.exit(app.exec_())
另一个解决方案:
class UiDialog(object):
def setupUi(self, datadb):
datadb.setObjectName("Dialog")
datadb.resize(404, 304)
datadb.setWindowTitle("Database results")
sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Fixed, QtGui.QSizePolicy.Fixed)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(datadb.sizePolicy().hasHeightForWidth())
datadb.setSizePolicy(sizePolicy)
datadb.setMinimumSize(QtCore.QSize(404, 304))
datadb.setMaximumSize(QtCore.QSize(404, 304))
self.table = QtGui.QTableWidget(datadb)
self.table.setGeometry(QtCore.QRect(2, 2, 400, 261))
self.table.setObjectName("table")
self.show = QtGui.QPushButton(datadb)
self.show.setGeometry(QtCore.QRect(2, 270, 400, 31))
self.show.setObjectName("show")
self.show.setText("Show results")
QtCore.QObject.connect(self.show, QtCore.SIGNAL("clicked()"), lambda: self.populate(datadb)) ### Using LAMBDA function to pass datadb object to the method
#self.show.pressed.connect(lambda: self.populate(datadb)) ### Another syntax for the same operation
QtCore.QMetaObject.connectSlotsByName(datadb)
def populate(self, mainWindow):
self.table.setRowCount(len(all_data))
self.table.setColumnCount(4)
self.table.setHorizontalHeaderLabels(['Number', 'Keys', 'Time', 'Tries'])
for i, item in enumerate(all_data):
number = QtGui.QTableWidgetItem(str(item[0]))
keys = QtGui.QTableWidgetItem(item[1])
time = QtGui.QTableWidgetItem(str(item[2]))
tries = QtGui.QTableWidgetItem(str(item[3]))
self.table.setItem(i, 0, number)
self.table.setItem(i, 1, keys)
self.table.setItem(i, 2, time)
self.table.setItem(i, 3, tries)
mainWindow.setWindowTitle("ANOTHER TITLE")
如果您使用的是从 .ui
文件生成的代码,您可以将主代码写入另一个文件,然后将生成的 ui 文件导入您的主代码,以便将来进行更改不会影响或更改您的主代码...所以我选择的最佳解决方案是:
from PyQt4 import QtCore, QtGui
from pyUI import UiDialog ### your UI file, pyUI is the file name and UiDialog is the Ui Class name
import sys
class GUI(QtGui.QDialog):
def __init__(self):
QtGui.QDialog.__init__(self)
self.ui = UiDialog()
self.ui.setupUi(self)
self.ui.show.pressed.connect(self.populate)
def populate(self):
self.ui.table.setRowCount(len(all_data))
self.ui.table.setColumnCount(4)
self.ui.table.setHorizontalHeaderLabels(['Number', 'Keys', 'Time', 'Tries'])
for i, item in enumerate(all_data):
number = QtGui.QTableWidgetItem(str(item[0]))
keys = QtGui.QTableWidgetItem(item[1])
time = QtGui.QTableWidgetItem(str(item[2]))
tries = QtGui.QTableWidgetItem(str(item[3]))
self.ui.table.setItem(i, 0, number)
self.ui.table.setItem(i, 1, keys)
self.ui.table.setItem(i, 2, time)
self.ui.table.setItem(i, 3, tries)
self.setWindowTitle("ANOTHER TITLE")
con = db.connect('results.db', isolation_level=None)
cur = con.cursor()
cur.execute("SELECT * FROM Results")
all_data = cur.fetchall()
app = QtGui.QApplication(sys.argv)
dailog = GUI()
dailog.show()
sys.exit(app.exec_())
self.ui.btn_close.clicked.connect(self.btn_close_clicked)
def btn_close_clicked(自我):
self.close()
我制作了一个非常简单的 python 脚本来将 sqlite3 数据库读入 QtTableWidget。
import sqlite3 as db
from PyQt4 import QtCore, QtGui
import sys
con = db.connect('results.db', isolation_level=None)
cur = con.cursor()
cur.execute("SELECT * FROM Results")
all_data = cur.fetchall()
class UiDialog(object):
def setupUi(self, datadb):
datadb.setObjectName("Dialog")
datadb.resize(404, 304)
datadb.setWindowTitle("Database results")
sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Fixed, QtGui.QSizePolicy.Fixed)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(datadb.sizePolicy().hasHeightForWidth())
datadb.setSizePolicy(sizePolicy)
datadb.setMinimumSize(QtCore.QSize(404, 304))
datadb.setMaximumSize(QtCore.QSize(404, 304))
self.table = QtGui.QTableWidget(datadb)
self.table.setGeometry(QtCore.QRect(2, 2, 400, 261))
self.table.setObjectName("table")
self.show = QtGui.QPushButton(datadb)
self.show.setGeometry(QtCore.QRect(2, 270, 400, 31))
self.show.setObjectName("show")
self.show.setText("Show results")
QtCore.QObject.connect(self.show, QtCore.SIGNAL("clicked()"), self.populate)
QtCore.QMetaObject.connectSlotsByName(datadb)
def populate(self):
self.table.setRowCount(len(all_data))
self.table.setColumnCount(4)
self.table.setHorizontalHeaderLabels(['Number', 'Keys', 'Time', 'Tries'])
for i, item in enumerate(all_data):
number = QtGui.QTableWidgetItem(str(item[0]))
keys = QtGui.QTableWidgetItem(item[1])
time = QtGui.QTableWidgetItem(str(item[2]))
tries = QtGui.QTableWidgetItem(str(item[3]))
self.table.setItem(i, 0, number)
self.table.setItem(i, 1, keys)
self.table.setItem(i, 2, time)
self.table.setItem(i, 3, tries)
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
main_conf = QtGui.QDialog()
ui = UiDialog()
ui.setupUi(main_conf)
main_conf.show()
ret = app.exec_()
sys.exit(ret)
它工作正常。我现在的问题是:
有什么方法可以在 运行 时间动态更改 window 标题吗?
setWindowTitle() 不会即时更新标题栏,update() 或 repaint() 也不会。
非常感谢您抽出宝贵的时间!
def setupUi(self, datadb):
[...]
self.datadb = datadb
def populate(self):
[...]
self.datadb.setWindowTitle('new title')
这行得通。但是您正在以一种奇怪且笨拙的方式构建您的小部件。比较常规的是使用继承:
class DatabaseUi(QtGui.QDialog):
def __init__(self):
QtGui.QDialog.__init__(self)
self.table = QtGui.QTableWidget()
self.button = QtGui.QPushButton()
self.button.setText("Show results")
self.button.clicked.connect(self.populate)
vlay = QtGui.QVBoxLayout()
vlay.addWidget(self.table)
vlay.addWidget(self.button)
self.setLayout(vlay)
self.setFixedSize(404, 304)
self.setWindowTitle("Database results")
def populate(self):
self.setWindowTitle('new title')
self.table.setColumnCount(4)
self.table.setHorizontalHeaderLabels(['Number', 'Keys', 'Time', 'Tries'])
[...]
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
ui = DatabaseUi()
ui.show()
sys.exit(app.exec_())
另一个解决方案:
class UiDialog(object):
def setupUi(self, datadb):
datadb.setObjectName("Dialog")
datadb.resize(404, 304)
datadb.setWindowTitle("Database results")
sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Fixed, QtGui.QSizePolicy.Fixed)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(datadb.sizePolicy().hasHeightForWidth())
datadb.setSizePolicy(sizePolicy)
datadb.setMinimumSize(QtCore.QSize(404, 304))
datadb.setMaximumSize(QtCore.QSize(404, 304))
self.table = QtGui.QTableWidget(datadb)
self.table.setGeometry(QtCore.QRect(2, 2, 400, 261))
self.table.setObjectName("table")
self.show = QtGui.QPushButton(datadb)
self.show.setGeometry(QtCore.QRect(2, 270, 400, 31))
self.show.setObjectName("show")
self.show.setText("Show results")
QtCore.QObject.connect(self.show, QtCore.SIGNAL("clicked()"), lambda: self.populate(datadb)) ### Using LAMBDA function to pass datadb object to the method
#self.show.pressed.connect(lambda: self.populate(datadb)) ### Another syntax for the same operation
QtCore.QMetaObject.connectSlotsByName(datadb)
def populate(self, mainWindow):
self.table.setRowCount(len(all_data))
self.table.setColumnCount(4)
self.table.setHorizontalHeaderLabels(['Number', 'Keys', 'Time', 'Tries'])
for i, item in enumerate(all_data):
number = QtGui.QTableWidgetItem(str(item[0]))
keys = QtGui.QTableWidgetItem(item[1])
time = QtGui.QTableWidgetItem(str(item[2]))
tries = QtGui.QTableWidgetItem(str(item[3]))
self.table.setItem(i, 0, number)
self.table.setItem(i, 1, keys)
self.table.setItem(i, 2, time)
self.table.setItem(i, 3, tries)
mainWindow.setWindowTitle("ANOTHER TITLE")
如果您使用的是从 .ui
文件生成的代码,您可以将主代码写入另一个文件,然后将生成的 ui 文件导入您的主代码,以便将来进行更改不会影响或更改您的主代码...所以我选择的最佳解决方案是:
from PyQt4 import QtCore, QtGui
from pyUI import UiDialog ### your UI file, pyUI is the file name and UiDialog is the Ui Class name
import sys
class GUI(QtGui.QDialog):
def __init__(self):
QtGui.QDialog.__init__(self)
self.ui = UiDialog()
self.ui.setupUi(self)
self.ui.show.pressed.connect(self.populate)
def populate(self):
self.ui.table.setRowCount(len(all_data))
self.ui.table.setColumnCount(4)
self.ui.table.setHorizontalHeaderLabels(['Number', 'Keys', 'Time', 'Tries'])
for i, item in enumerate(all_data):
number = QtGui.QTableWidgetItem(str(item[0]))
keys = QtGui.QTableWidgetItem(item[1])
time = QtGui.QTableWidgetItem(str(item[2]))
tries = QtGui.QTableWidgetItem(str(item[3]))
self.ui.table.setItem(i, 0, number)
self.ui.table.setItem(i, 1, keys)
self.ui.table.setItem(i, 2, time)
self.ui.table.setItem(i, 3, tries)
self.setWindowTitle("ANOTHER TITLE")
con = db.connect('results.db', isolation_level=None)
cur = con.cursor()
cur.execute("SELECT * FROM Results")
all_data = cur.fetchall()
app = QtGui.QApplication(sys.argv)
dailog = GUI()
dailog.show()
sys.exit(app.exec_())
self.ui.btn_close.clicked.connect(self.btn_close_clicked)
def btn_close_clicked(自我): self.close()