gpt4 book ai didi

python - Q设置(): How to save to current working directory

转载 作者:太空狗 更新时间:2023-10-29 20:59:35 26 4
gpt4 key购买 nike

对于可以直接从闪存/笔/USB/jump/拇指驱动器运行的应用程序,为了从一台机器移动到另一台机器的便携性,将用户设置存储在内存棒上是有意义的程序运行的目录(而不是每台机器的 Windows/Mac/Linux 用户或系统目录)。

QSettings() 很方便,但是,可以告诉它使用当前工作目录吗?

这是一个小示例程序,它使用 QSettings() 来保持其屏幕位置在运行之间:

from PySide import QtGui, QtCore
from PySide.QtGui import QTabWidget, QApplication
from PySide.QtCore import QSettings

class AbcApp(QTabWidget):
def __init__(self):
super(AbcApp, self).__init__()

self.settings = QSettings(QSettings.IniFormat,QSettings.SystemScope, '__MyBiz', '__settings')
self.settings.setFallbacksEnabled(False) # File only, not registry or or.

# setPath() to try to save to current working directory
self.settings.setPath(QSettings.IniFormat,QSettings.SystemScope, './__settings.ini')

# Initial window size/pos last saved
self.resize(self.settings.value("size", QtCore.QSize(270, 225)))
self.move(self.settings.value("pos", QtCore.QPoint(50, 50)))

self.tab = QtGui.QWidget()
self.textEdit = QtGui.QTextEdit(self.tab)
self.textEdit.setHtml('<font color=grey>[QTextEdit area]</font><p><center><font color=blue size=4><b>Allo Woyld</b></font></center>')
self.addTab(self.tab, 'tab1')

def closeEvent(self, e):
# Write window size and position to config file
self.settings.setValue("size", self.size())
self.settings.setValue("pos", self.pos())

e.accept()

if __name__ == '__main__':
app = QApplication('')
frame = AbcApp()
frame.show()
app.exec_()

创建此 .ini 文件是因为我此时恰好在 Windows 上运行:C:\Documents and Settings\All Users\Application Data__MyBiz__settings.ini

UserScope 而不是 SystemScope 没有帮助。
'.' 而不是 './__settings.ini' 不起作用,setPath() 基本上没有效果。
也试过这个无济于事:

filepath = os.getcwd() + '/__settings.ini'
self.settings.setPath(QSettings.IniFormat,QSettings.SystemScope, filepath)

引用:https://doc.qt.io/archives/qt-4.8/qsettings.html http://www.pyside.org/docs/pyside/PySide/QtCore/QSettings.html

虽然我不知道如何适应 PySide,但很有前途:
http://www.qtcentre.org/archive/index.php/t-35287.html


更新:来自 alexisdm 的答案有效,所以这里是更新后的代码:

from PySide import QtGui, QtCore
from PySide.QtGui import QTabWidget, QApplication
from PySide.QtCore import QSettings

class AbcApp(QTabWidget):
def __init__(self):
super(AbcApp, self).__init__()

self.settings = QSettings('settings.ini', QSettings.IniFormat)
self.settings.setFallbacksEnabled(False) # File only, no fallback to registry or or.

# Initial window size/pos last saved if available
self.resize(self.settings.value('size', QtCore.QSize(270, 225)))
self.move(self.settings.value('pos', QtCore.QPoint(50, 50)))

self.tab = QtGui.QWidget()
self.textEdit = QtGui.QTextEdit(self.tab)
self.textEdit.setHtml('<font color=grey>[QTextEdit area]</font><p><center><font color=blue size=4><b>Allo Woyld</b></font></center>')
self.addTab(self.tab, 'tab1')

def closeEvent(self, e):
self.settings.setValue('size', self.size())
self.settings.setValue('pos', self.pos())
e.accept()

if __name__ == '__main__':
app = QApplication('')
frame = AbcApp()
frame.show()
app.exec_()

最佳答案

您可以像这样使用重载 class QSettings(fileName, format[, parent=None]):

self.settings = QSettings("__settings.ini", QSettings.IniFormat)

如果路径是相对路径,文件将已经在当前工作目录中打开,但这可能不是您想要的。
你可以试试these answers之一使用脚本所在的目录。

关于python - Q设置(): How to save to current working directory,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7962292/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com