字典和pyqt

时间:2014-02-16 18:49:06

标签: python pyqt pyqtgraph

我正在使用pyqt和pyqtgraph进行GUI。在我的应用程序中,我有10个选项卡式imageview,我想知道如何转换以下内容,这需要更多优雅的pythonic方式来处理这些对象。所以不要这样:

img1 = pg.ImageView() 
img2 = pg.ImageView() 
img3 = pg.ImageView()
img4 = pg.ImageView()
img5 = pg.ImageView()
img6 = pg.ImageView()
img7 = pg.ImageView()
img8 = pg.ImageView()
img9 = pg.ImageView()
img10 = pg.ImageView()


tab_widget = QtGui.QTabWidget() 
tab1 = QtGui.QWidget() 
tab2 = QtGui.QWidget() 
tab3 = QtGui.QWidget() 
tab4 = QtGui.QWidget() 
tab5 = QtGui.QWidget() 
tab6 = QtGui.QWidget() 
tab7 = QtGui.QWidget() 
tab8 = QtGui.QWidget() 
tab9 = QtGui.QWidget() 
tab10 = QtGui.QWidget() 

p1_vertical = QtGui.QVBoxLayout(tab1) 
p2_vertical = QtGui.QVBoxLayout(tab2) 
p3_vertical = QtGui.QVBoxLayout(tab3) 
p4_vertical = QtGui.QVBoxLayout(tab4) 
p5_vertical = QtGui.QVBoxLayout(tab5) 
p6_vertical = QtGui.QVBoxLayout(tab6) 
p7_vertical = QtGui.QVBoxLayout(tab7) 
p8_vertical = QtGui.QVBoxLayout(tab8) 
p9_vertical = QtGui.QVBoxLayout(tab9) 
p10_vertical = QtGui.QVBoxLayout(tab10) 

tab_widget.addTab(tab1, "Grid 1") 
tab_widget.addTab(tab2, "Grid 2")
tab_widget.addTab(tab3, "Grid 3") 
tab_widget.addTab(tab4, "Grid 4") 
tab_widget.addTab(tab5, "Grid 5") 
tab_widget.addTab(tab6, "Grid 6") 
tab_widget.addTab(tab7, "Grid 7") 
tab_widget.addTab(tab8, "Grid 8") 
tab_widget.addTab(tab9, "Grid 9") 
tab_widget.addTab(tab10, "Grid 10")  

button1 = QtGui.QPushButton("button1") 
p1_vertical.addWidget(img1)
p2_vertical.addWidget(img2) 
p3_vertical.addWidget(img3) 
p4_vertical.addWidget(img4) 
p5_vertical.addWidget(img5) 
p6_vertical.addWidget(img6) 
p7_vertical.addWidget(img7) 
p8_vertical.addWidget(img8) 
p9_vertical.addWidget(img9)  
p10_vertical.addWidget(img10) 

也许是这样的:

d = {} 
for i in range(1,10):
    d["img{0}".format(i)]= pg.ImageView()

3 个答案:

答案 0 :(得分:2)

imgs = [pg.ImageView() for _ in range(10)]

tab_widget = QtGui.QTabWidget()

tabs = [QtGui.QWidget() for _ in range(10)]

p_verticals = [QtGui.QVBoxLayout(tab) for tab in tabs]

for n, tab in enumerate(tabs):
    tab_widget.addTab(tab, "Grid {0}".format(n+1))

button1 = QtGui.QPushButton("button1")

for p, img in zip(p_verticals, imgs):
    p.addWidget(img)

我不明白你为什么要使用字典,列表很好。

答案 1 :(得分:2)

创建一个自定义窗口小部件类,在其__init__中执行所有设置:

class ImageViewTab(QtGui.QWidget):
    def __init__(self, parent=None)
        super(ImageViewTab, self).__init__(parent)
        layout = QtGui.QVBoxLayout(self)
        self.imageview = pg.ImageView(self)
        layout.addWidget(self.imageview)

然后将小部件的实例添加到循环中的tab-widget:

    tab_widget = QtGui.QTabWidget()
    for index in range(1, 11):
        widget = ImageViewTab(tab_widget)
        tab_widget.addTab(widget, 'Grid %s' % index)

完成所有设置后,您可以使用QTabWidget.widget方法按索引访问小部件:

    widget = tab_widget.widget(4)
    widget.imageview.setImage(img)

这种方法的主要好处是它非常灵活。您可以向自定义类添加专用方法,创建和发出自定义信号,添加插槽等。

答案 2 :(得分:1)

您可以在列表中创建所有变量:

    imgs = [pg.ImageView() for _ in range(10)]
    tab_widget = QtGui.QTabWidget() 
    tabs = [QtGui.QWidget() for _ in range(10)]
    p_verticals = [QtGui.QVBoxLayout(tabs[i]) for i in range(10)]
    for i in range(10):
        tab_widget.addTab(tab[i], "Grid %d" % i+1) 
        p_verticals[i].addWidget(imgs[i])
    button1 = QtGui.QPushButton("button1")