我有一个配置对话框,我预先填充了现有选项,这些选项存储在cfg
中。当用户单击“保存”(或等效)时,我需要从QLineEdit对象中获取新值。除了我无法想象那一点。从昨天晚上起,我一直在谷歌搜索和测试,然后我再次弯曲膝盖来到这里。这是我的对话框代码(表单来自Qt Designer,这就是为什么没有GUI代码):
class Config(QDialog):
def __init__(self):
super(Config, self).__init__()
popup = QDialog()
config_ui = configform()
config_ui.setupUi(popup)
config_ui.programver.setText(cfg['config']['programver'])
if cfg['config']['dummycopy']:
config_ui.democheck.setChecked(True)
config_ui.tmdbAPIkey.setText(cfg['config']['TMDB_KEY'])
config_ui.tvdbAPIkey.setText(cfg['config']['TVDB_KEY'])
config_ui.tvdbUserkey.setText(cfg['config']['TVDB_USERKEY'])
theme = cfg['config']['theme']
if theme == "blue":
config_ui.bluebutton.setChecked(True)
elif theme == "yellow":
config_ui.yellowbutton.setChecked(True)
elif theme == "light":
config_ui.lightmetalbutton.setChecked(True)
elif theme == "dark":
config_ui.darkmetalbutton.setChecked(True)
programversion = config_ui.programver.text()
config_ui.savebutton.clicked.connect(lambda: Config.save(self, programversion))
popup.exec_()
def save(self, programversion):
QDialog.close(self)
print(programversion)
我需要一些伏都教才能进入变化的领域。我现在能得到的只是对话框生动时的原始值。这有诀窍吗?我不能成为第一个尝试预先填充对话框的人。我发誓我已经尝试了按钮和buttonBox变体的每种组合。
我想也许有一些方法可以隐藏对话框,抓取数据,然后破坏对话框?无论如何,这是一个有效的理论。
提前致谢。
答案 0 :(得分:0)
为了以简单的方式工作,我们使用Qt Designer的设计来填充Dialog,我们将cancel按钮连接到self.reject(),将save按钮连接到save()插槽,在此我们保存数据并发出self.accept():
from PyQt5.QtWidgets import *
from Ui_config_dialog import Ui_configdialog
import configparser
class Config(QDialog, Ui_configdialog):
def __init__(self, *args, **kwargs):
QDialog.__init__(self, *args, **kwargs)
self.setupUi(self)
self.cancelbutton.clicked.connect(self.reject)
self.filename = "joe.conf"
self.cfg = configparser.ConfigParser()
self.cfg.read(self.filename)
self.load()
def load(self):
self.programver.setText(self.cfg['config']['programver'])
self.democheck.setChecked(self.cfg.getboolean("config", "dummycopy"))
self.tmdbAPIkey.setText(self.cfg['config']['TMDB_KEY'])
self.tvdbAPIkey.setText(self.cfg['config']['TVDB_KEY'])
self.tvdbUserkey.setText(self.cfg['config']['TVDB_USERKEY'])
theme = self.cfg['config']['theme']
self.buttons = {"blue": self.bluebutton,
"yellow": self.yellowbutton,
"light": self.lightmetalbutton,
"dark": self.darkmetalbutton}
self.buttons[theme].setChecked(True)
self.group = QButtonGroup(self)
self.group.addButton(self.bluebutton)
self.group.addButton(self.yellowbutton)
self.group.addButton(self.lightmetalbutton)
self.group.addButton(self.darkmetalbutton)
self.savebutton.clicked.connect(self.save)
def save(self):
self.cfg['config']['programver'] = self.programver.text()
self.cfg['config']['dummycopy'] = "True" if self.democheck.isChecked() else "False"
self.cfg['config']['TMDB_KEY'] = self.tmdbAPIkey.text()
self.cfg['config']['TVDB_KEY'] = self.tvdbUserkey.text()
for key, btn in self.buttons.items():
if btn == self.group.checkedButton():
self.cfg['config']['theme'] = key
break
with open(self.filename, 'w') as configfile:
self.cfg.write(configfile)
self.accept()
if __name__ == "__main__":
import sys
app = QApplication(sys.argv)
w = Config()
w.show()
sys.exit(app.exec_())