2017-12-07 11 views
0

私は、既存のオプションで事前設定している設定ダイアログを持っています。これはcfgに保存されています。ユーザーが「保存」(または同等のもの)をクリックすると、QLineEditオブジェクトから新しい値を取得する必要があります。私はそれを理解できないことを除いて。私は昨日夕方からグーグルでテストしていました。PyQt5から変更されたデータを取得QDialog

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) 

私が変更されたフィールドで取得するには、いくつかのブードゥー教が必要になります。ここに私のダイアログのコードは、(フォームにはGUIのコードはありません理由ですQtのデザイナー、からである)です。私が今取得できるのは、ダイアログが生まれてからの元の値です。これにトリックはありますか?ダイアログボックスをあらかじめ設定しておくのは初めての人にはなりません。私は、ボタンとボタンボックスのバリエーションを組み合わせて試してみたことを誓っています。

ダイアログを隠してデータを取得してからダイアログを破棄する方法があると思いますか?とにかくそれは一つの働く理論です。

ありがとうございます。

+0

cfgに加えて、Qt Designerの設計を共有してください。 – eyllanesc

+0

configファイルの要点(ConfigParserモジュールが読む)、config.uiのXML、生成されたPythonファイル... https://gist.github.com/bundito/e6656928dedc61fa45fa2e1b90b18b12 – Dito

+0

私の答えを参照してください。 – eyllanesc

答えて

0

簡単な方法で作業するには、Qt Designerのデザインを使用してダイアログを完成させ、キャンセルボタンをself.reject()に、保存ボタンを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_()) 
+0

申し訳ありませんが、プロジェクトに戻ってコードを実装するにはしばらく時間がかかりました。 'save()'関数は私が探していた魔法です。ありがとうございました。答えは正しいとマークされています! – Dito

関連する問題