2017-12-07 4 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는 디자이너 출신)이다. 지금 내가 얻을 수있는 것은 대화가 현실화되었을 때의 원래 값입니다. 이 트릭이 있습니까? 대화 상자를 미리 채울 수있는 첫 번째 사람이 될 수 없습니다. 나는 button과 buttonBox 변형의 모든 조합을 시도해 봤다.

대화 상자를 숨기고 데이터를 가져온 다음 대화 상자를 파괴 할 수있는 방법이 있을지 모릅니다. 그것은 어쨌든 하나의 작동 이론입니다.

미리 감사드립니다.

+0

당신을 도울 cfg 이외에 Qt 디자이너의 디자인을 공유하십시오. – eyllanesc

+0

config 파일의 개요 (ConfigParser 모듈에서 읽음), config.ui의 XML 및 생성 된 Python 파일 ... https://gist.github.com/bundito/e6656928dedc61fa45fa2e1b90b18b12 – Dito

+0

내 대답보기. – eyllanesc

답변

0

간단한 방법으로 작업하려면 Qt Designer의 디자인을 사용하여 대화 상자를 채우고 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_()) 
+0

미안 해요. 프로젝트로 돌아가서 코드를 구현하는 데 시간이 좀 걸렸습니다. 'save()'함수는 내가 찾고 있던 마법이다. 고맙습니다. 정확한 답으로 표시하십시오! – Dito