python - PyQt : Compiling date/time into window title

标签 python python-3.x pyqt5 pyinstaller

如何将编译日期/时间放入窗口标题中? 下面的示例(当然)不起作用,因为它会将程序启动时的日期和时间放入标题中。 好吧,每次编译新版本时,我都可以在源代码中手动输入日期和时间。然而,我希望有一个智能和自动化的解决方案。有什么想法吗?

代码:

import sys
from datetime import datetime
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QMessageBox 

class MyWindow(QWidget):
    def __init__(self):
        super(MyWindow,self).__init__()
        self.setGeometry(100,100,400,200)
        self.setWindowTitle('Version ' + datetime.now().strftime("%Y%m%d-%H%M"))
        # define button1 
        self.button1 = QPushButton('Push me',self)
        self.button1.move(100,100)
        self.button1.setStyleSheet("background-color: #ddffdd")
        self.button1.clicked.connect(self.button1_clicked)

    def button1_clicked(self):
        msg = QMessageBox()
        msg.setText("This is a message box")
        msg.exec_()

if __name__ == '__main__':
    app = QApplication(sys.argv)
    app.setStyle("plastique")
    window = MyWindow()
    window.show()
    sys.exit(app.exec_())

编译:

pyinstaller --onefile tbMinimalPyQt5.py

.spec 文件

# -*- mode: python -*-

block_cipher = None


a = Analysis(['tbMinimalPyQt5.py'],
             pathex=['C:\\User\\Test'],
             binaries=[],
             datas=[],
             hiddenimports=[],
             hookspath=[],
             runtime_hooks=[],
             excludes=[],
             win_no_prefer_redirects=False,
             win_private_assemblies=False,
             cipher=block_cipher)
pyz = PYZ(a.pure, a.zipped_data,
             cipher=block_cipher)
exe = EXE(pyz,
          a.scripts,
          a.binaries,
          a.zipfiles,
          a.datas,
          name='tbMinimalPyQt5',
          debug=False,
          strip=False,
          upx=True,
          runtime_tmpdir=None,
          console=True )

最佳答案

一种解决方案是在 .spec 中编写一段代码,用数据创建 .py:

# -*- mode: python -*-

import datetime

data = {
  "BUILD_DATE": datetime.datetime.now().strftime("%Y%m%d-%H%M")
}

with open('constants.py', 'w') as f:
    for k, v in data.items():
      f.write("{} = \"{}\"\n".format(k, v))

block_cipher = None


a = Analysis(['tbMinimalPyQt5.py'],
             pathex=['C:\\User\\Test'],
# ...

然后在 .py 中使用编译时生成的值导入文件constants.py:

import sys
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QMessageBox 
import constants

class MyWindow(QWidget):
    def __init__(self):
        super(MyWindow,self).__init__()
        self.setGeometry(100,100,400,200)
        self.setWindowTitle('Version ' + constants.BUILD_DATE)
        # define button1 
        self.button1 = QPushButton('Push me',self)
        self.button1.move(100,100)
        self.button1.setStyleSheet("background-color: #ddffdd")
        self.button1.clicked.connect(self.button1_clicked)

    def button1_clicked(self):
        msg = QMessageBox()
        msg.setText("This is a message box")
        msg.exec_()

if __name__ == '__main__':
    app = QApplication(sys.argv)
    app.setStyle("plastique")
    window = MyWindow()
    window.show()
    sys.exit(app.exec_())

最后使用 .spec 进行编译:

pyinstaller tbMinimalPyQt5.spec

关于python - PyQt : Compiling date/time into window title,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54820770/

相关文章:

python - groupby 并对两列求和并在 pandas 中设置为一列

python-3.x - 如何在 Mac m1 上使用 homebrew 安装 python 3.9.6

python - 如何使QMessageBox和QInputDialog在屏幕上居中?

python - 如何使用 QtCore.Qt.UserRole 在 QListWidgetItem 中显示部分粗体文本

python - rpy2代码评估方法之间的区别

python - 我的代码将 append 一个列表,但不会打印出正确的长度

python - Pandas.read_sql 失败,出现 DBAPIError 07002 : COUNT field incorrect or syntax error

python - C++ 到 Python 初学者

python - Heroku 部署 : dash. 异常。NoLayoutException

python - 在PyQt5中用于显示图像的类