我有一个以qmainwindow开头的应用程序。在为数据库和其他东西选择合适的位置(目录)后,它允许用户启动plc控制器的设置,该控制器位于第二个窗口(qwidget)上。到目前为止没有问题。
在第二个窗口(setupwindow)构建的同时,它还检查一系列xml文件,对它们进行解析,构建字典等。
如果在解析时遇到错误,我想捕获它,请停止安装窗口完成填充表、列表等,然后弹出第三个窗口,显示解析错误,并允许用户编辑坏的xml并修复它(启动所选的文本编辑器)。
每一个空模块都可以正常工作(小部件驻留在不同的模块上)。当我把它们放在一起的时候,在解析异常的情况下,第三个窗口没有显示,应用程序继续填充第二个窗口(部分是在这个时候,因为一些xml由于格式错误而被跳过,这就是我试图捕捉的异常)。
奇怪的是,当我尝试从pycharm调试它时,在异常处中断,然后进入,第三个窗口会定期弹出。我做错什么了?
这是我的代码(注意:我使用qtdesigner来设置ui布局)。
第一个主窗口(摘要:单击按钮的事件以启动设置):
def setupOnClick(self):
config['active']['old_gui'] = self.ui.GUI_old_proj_path_textline.text()
config['active']['controllers'] = self.ui.controllers_files_path_textline.text()
config['active']['proj_dest'] = self.ui.dest_folder_textline.text()
config_name = os.path.join(this_app_path, 'config.ini')
with open(config_name, "w") as config_file:
config.write(config_file)
import SetupControllers
self.setupWin = SetupControllers.MainWindow(config,
config_name,
self.main_icon,
MainWindow.CurProJectName)
这是第二个窗口(设置控制器):
from lib.SearchControllers import get_controller_dict # this is an external module where I stuff various functions
from PyQt5 import QtWidgets as qtw
from PyQt5 import QtGui as qtg
myForm_2, baseClass = uic.loadUiType('./forms/setup.ui')
class MainWindow(baseClass):
def __init__(self, config_obj: ConfigParser,
config_name: str,
icon: qtg.QIcon(),
proj_name: str,
*args,
**kwargs):
super().__init__(*args,**kwargs)
self.ui = myForm_2()
self.ui.setupUi(self)
self.setWindowIcon(icon)
self.setWindowTitle(proj_name + " Setup")
self.root_path = self.config_obj['active']['controllers']
# doing some more init stuff
# .........................
self.load_bulk() # starting to parse xml dbase to populate the various widgets
self.show
以下是调用解析函数的位置:
def load_bulk(self):
# get the list of running components into a dictionary
for i in self.list_controllers:
i_path = os.path.join(self.root_path, i)
self.components[i] = get_controller_dict(i_path,
self.config_obj,
'Application.xml',
'Subcomponents/Subcomponent',
'Name',
'src')
下面是解析函数,我想在其中捕获异常:
import lib.parsingError as PE
def get_controller_dict(controller_path: str,
config: ConfigParser,
target: str,
inner_element: str,
tag_key: str,
tag_value: str,
tag_value_2: str=None):
gc_dict = {}
cdp_app_file = os.path.join(controller_path, target)
if not os.path.exists(cdp_app_file):
return gc_dict
try:
root = ET.parse(cdp_app_file).getroot()
for type_tag in root.findall(inner_element):
gc_dict[type_tag.attrib[tag_key]] = type_tag.attrib[tag_value]
except ET.ParseError as ex: # debug breakpoint is here
parseErr = PE.MainWindow(config, ex, cdp_app_file)
except Exception as ex:
show_exception(f"Parsing {cdp_app_file}", ex)
return gc_dict
最后是pe.parsing错误。同样,它也驻留在自己的模块中:
from PyQt5 import uic
from PyQt5 import QtWidgets as qtw
from PyQt5 import QtGui as qtg
from configparser import ConfigParser
myForm_3, baseClass = uic.loadUiType('./forms/parseErr.ui')
class MainWindow(baseClass):
def __init__(self, config_obj: ConfigParser,
exc: ET.ParseError,
filename: str,
*args,
**kwargs):
super().__init__(*args,**kwargs)
# your code begins here
self._icon = qtg.QIcon()
self.ui = myForm_3()
self.ui.setupUi(self)
self.ui.MM_logo.setPixmap(qtg.QPixmap(':/logo_Small.png'))
self.main_icon = qtg.QIcon()
self.main_icon.addPixmap(qtg.QPixmap(':/logo_Small.png'), qtg.QIcon.Normal, qtg.QIcon.Off)
self.setWindowIcon(self.main_icon)
self.exc = exc
self.config = config_obj
self.filename = filename
self.setLabels()
self.ui.OK_butt.clicked.connect(self.ok_onClick)
self.ui.validate_butt.clicked.connect(self.validate_onClick)
self.ui.editXml_butt.clicked.connect(self.editXML)
# your code ends here
self.show()
暂无答案!
目前还没有任何答案,快来回答吧!