RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / 问题 / 1103388
Accepted
MAXIM045
MAXIM045
Asked:2020-03-31 21:14:58 +0000 UTC2020-03-31 21:14:58 +0000 UTC 2020-03-31 21:14:58 +0000 UTC

如何优化容器的创建?

  • 772

在循环体中,我需要创建不少具有完全相同特征但内容不同的容器。

这目前分三个阶段进行:

  1. 已创建QWidget
  2. 某些外部特性被设置为QWidget
  3. 一个新的容器被创建并绑定到QWidget

循环重复一定次数(总是不同的)。

这让我很感兴趣,也许可以提前为容器制作一个模板,而不是每次都重新创建它?如果可能,请帮我优化代码。

from PyQt5 import QtCore, QtWidgets, QtGui

class MyWindow(QtWidgets.QWidget):
    def __init__(self, parent = None):
        super().__init__(parent)

        self.main_box = QtWidgets.QVBoxLayout(self)

        self.make()

    def make(self):
        for i in range(0, 5):
            container = QtWidgets.QWidget()
            container.setStyleSheet(box_qss)
            container.setFixedHeight(50)
            song_box = QtWidgets.QHBoxLayout(container)
            self.main_box.addWidget(container)        


box_qss = '''QWidget {
                 background-color: #1F252F;
                 border-radius: 5px;
             }'''


if __name__ == '__main__':
    import sys
    app = QtWidgets.QApplication(sys.argv)
    window = MyWindow()
    window.setWindowTitle(' ')
    window.show()
    sys.exit(app.exec_())

UPD:每个容器将有一个按钮和两个铭文(每个铭文的文字会有所不同):

from PyQt5 import QtCore, QtWidgets, QtGui

class MyWindow(QtWidgets.QWidget):
    def __init__(self, parent = None):
        super().__init__(parent)

        self.main_box = QtWidgets.QVBoxLayout(self)

        self.make()

    def make(self):
        for i in range(0, 5):
            container = QtWidgets.QWidget()
            container.setStyleSheet(box_qss)
            container.setFixedHeight(50)
            song_box = QtWidgets.QHBoxLayout(container)
            self.main_box.addWidget(container)

            btn = QtWidgets.QPushButton('Button')
            btn.setStyleSheet(btn_qss)
            song_box.addWidget(btn)

            label = QtWidgets.QLabel('Text')
            label.setStyleSheet(label_qss)
            song_box.addWidget(label)

            duration = QtWidgets.QLabel('00:00')
            duration.setStyleSheet(label_qss)
            song_box.addWidget(duration)


box_qss = '''QWidget {
                 background-color: #1F252F;
                 border-radius: 5px;
             }'''

label_qss = '''QLabel {
                   color: white;
               }'''

btn_qss = '''QPushButton {
                 background-color: #3A4256;
                 color: white;
             }'''


if __name__ == '__main__':
    import sys
    app = QtWidgets.QApplication(sys.argv)
    window = MyWindow()
    window.setWindowTitle(' ')
    window.show()
    sys.exit(app.exec_())
python
  • 1 1 个回答
  • 10 Views

1 个回答

  • Voted
  1. Best Answer
    S. Nick
    2020-04-01T02:56:32Z2020-04-01T02:56:32Z

    我建议这样尝试:

    from random import randint
    from PyQt5 import QtCore, QtWidgets, QtGui
    
    class Widget(QtWidgets.QWidget):
        def __init__(self, list_album, parent=None):
            super(Widget, self).__init__(parent)
    
            self.song_box = QtWidgets.QGridLayout(self)
            self.song_box.setContentsMargins(0, 0, 0, 0)
    
            for i, text in enumerate(list_album):
                button = QtWidgets.QPushButton(f"Button {i+1}", 
                    minimumWidth=70,             
                    minimumHeight=50,
                    clicked=lambda ch, t=text[0]: self.onButton(t))  
                label = QtWidgets.QLabel(
                    f'{text[0]} - {text[1]}', alignment = QtCore.Qt.AlignCenter)
    
                self.song_box.addWidget(button, i, 0)
                self.song_box.addWidget(label, i, 1)
    
        def onButton(self, text):
            print(f"Выбрали песню {text}")
    
    
    class MyWindow(QtWidgets.QWidget):
        def __init__(self, music_album, parent = None):
            super(MyWindow,self).__init__(parent)
    
            self.music_album = music_album
            self.container = None
    
            self.main_box = QtWidgets.QGridLayout(self)
            self.button = QtWidgets.QPushButton('Выберите Music Album', minimumHeight=50, clicked=self.make)
            self.main_box.addWidget(self.button, 111, 0, QtCore.Qt.AlignBottom)
    
        def make(self):
            num_album = randint(1, len(self.music_album))    # выбираем случайный альбом
            list_album = self.music_album[num_album]
            if self.container:
                self.container.deleteLater()
    
            self.container = Widget(list_album, self)
            self.main_box.addWidget(self.container, 1, 0)
    
    
    
    qss = '''
    QWidget {
        background-color: #1F252F;
        border-radius: 5px;
    }
    QLabel {
        color: white;
    }
    QPushButton {
        background-color: #3A4256;
        color: white;
    }
    '''
    
    # Какие-то альбомы с какими-то песнями
    music_album = {
    1: (('Text1', '00:00'), ('Text2', '00:02'), ('Text3', '00:03'), ('Text4', '00:04'), ('Text5', '00:05'),),
    2: (('Text21', '00:00'), ('Text22', '00:02'), ('Text23', '00:03'), ('Text24', '00:03'),),
    3: (('Text31', '00:00'), ('Text32', '00:02'), ('Text33', '00:03'), ),
    4: (('Text41', '00:00'), ('Text42', '00:02'),),
    5: (('Text51', '00:00'),),
    }
    
    
    if __name__ == '__main__':
        import sys
        app = QtWidgets.QApplication(sys.argv)
        app.setStyleSheet(qss)                          # <<<=====<
        window = MyWindow(music_album)
        window.setWindowTitle(' ')
        window.resize(250, 370)
        window.show()
        sys.exit(app.exec_())
    

    在此处输入图像描述

    如果你有任何问题,写,我会评论。

    • 1

相关问题

  • 是否可以以某种方式自定义 QTabWidget?

  • telebot.anihelper.ApiException 错误

  • Python。检查一个数字是否是 3 的幂。输出 无

  • 解析多个响应

  • 交换两个数组的元素,以便它们的新内容也反转

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    如何从列表中打印最大元素(str 类型)的长度?

    • 2 个回答
  • Marko Smith

    如何在 PyQT5 中清除 QFrame 的内容

    • 1 个回答
  • Marko Smith

    如何将具有特定字符的字符串拆分为两个不同的列表?

    • 2 个回答
  • Marko Smith

    导航栏活动元素

    • 1 个回答
  • Marko Smith

    是否可以将文本放入数组中?[关闭]

    • 1 个回答
  • Marko Smith

    如何一次用多个分隔符拆分字符串?

    • 1 个回答
  • Marko Smith

    如何通过 ClassPath 创建 InputStream?

    • 2 个回答
  • Marko Smith

    在一个查询中连接多个表

    • 1 个回答
  • Marko Smith

    对列表列表中的所有值求和

    • 3 个回答
  • Marko Smith

    如何对齐 string.Format 中的列?

    • 1 个回答
  • Martin Hope
    Alexandr_TT 2020年新年大赛! 2020-12-20 18:20:21 +0000 UTC
  • Martin Hope
    Alexandr_TT 圣诞树动画 2020-12-23 00:38:08 +0000 UTC
  • Martin Hope
    Air 究竟是什么标识了网站访问者? 2020-11-03 15:49:20 +0000 UTC
  • Martin Hope
    Qwertiy 号码显示 9223372036854775807 2020-07-11 18:16:49 +0000 UTC
  • Martin Hope
    user216109 如何为黑客设下陷阱,或充分击退攻击? 2020-05-10 02:22:52 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5