【问题标题】:PyQt4: Which one of these would be better/more correct? (Notepad++ clone)PyQt4:其中哪一个会更好/更正确? (记事本++克隆)
【发布时间】:2013-09-27 21:27:14
【问题描述】:

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

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

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

我的代码cmets可以更好的解释。

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

【问题讨论】:

    标签: python oop pyqt4 procedural-programming


    【解决方案1】:

    如果您想知道是否使用字典类,您可能需要namedtuple。这为您提供了 dict 的简单性以及类的属性语法:

    from collections import namedtuple
    
    FooBar = namedtuple("FooBar", ["these", "are", "the", "attributes"])
    
    FooBar(123, 324, the=12, attributes=656).these
    #>>> 123
    

    【讨论】:

      猜你喜欢
      • 2013-10-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多