PyQt4:以下哪一个更好/更正确?(记事本++克隆)

2024-10-03 17:17:16 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在用Python/PyQt4制作一个超级简单的Notepad++克隆,我想知道存储编辑器选项卡数据的选项有哪些:

选项1:我有一个名为QQCodeTab的类,它存储当前Qsci.qscisintilla公司当前选项卡、文件路径、当前语言等的实例。这些由dict映射到选项卡索引

选项2:与选项1相同,但是去掉类并将所有内容存储在dict中(例如:{1: {"scintilla": <blah>, "filepath": "C:/File/whatevs.py"}, "language": "python"}

我的代码注释可以更好地解释它。你知道吗

from PyQt4 import QtGui, Qsci

class QQCodeEditor(QtGui.QTabWidget):
    def __init__(self, parent=None):
        QtGui.QTabWidget.__init__(self, parent)
        self.new_tab()
        self.new_tab()
        # Option 1: Maps index to tab object
        # Option 2: Maps index to dict of options
        self.tab_info = {}

    def new_tab(self):
        scin = Qsci.QsciScintilla()
        index = self.addTab(scin, "New Tab")

    def get_tab_info(self, index):
        # Returns QQCodeTab object
        return self.tab_info[index]

    def save(self, index):
        # Option 2: Save dialog boc and file system stuff goes here
        pass

class QQCodeTab(object):
    def __init__(self, scintilla, editor):
        self.scintilla = scintilla
        self.editor = editor

    def save(self):
        # Option 1: Save dialog box and file system stuff goes here
        pass

Tags: selfnewindexobjectinitdef选项tab
1条回答
网友
1楼 · 发布于 2024-10-03 17:17:16

如果您想知道是否要使用一类dictionary,您可能需要一个namedtuple。这使您能够使用类的属性语法简化dict

from collections import namedtuple

FooBar = namedtuple("FooBar", ["these", "are", "the", "attributes"])

FooBar(123, 324, the=12, attributes=656).these
#>>> 123

相关问题 更多 >