PyQt/PySide:基于JSon对象生成Button

问题描述 投票:0回答:0

我想根据 json 中的项目数量制作一个按钮生成器,每个水平布局有 4 个按钮。原因:主 UI 有代码重复,我希望它能够使用任何 json 模板。之前,我已经手动制作了 UI: Main Program

每个

QHBoxLayout
最多有4个按钮。代码确实重复:

    def content_submit():
        if controllers['current_products'].get('content') is not None:
            controllers['current_products']['content'] += 1
        else:
            controllers['current_products']['content'] = 1
            
        content_label.setText(str(controllers['current_products']['content']))
        controllers['current_prices'] += 4000
        moneys_label.setText(f'Total Moneys: {controllers["current_prices"]}')

    content_layout = QVBoxLayout()
    content_layout.setAlignment(Qt.AlignmentFlag.AlignLeft)
    content_button = QPushButton()
    content_label = QLabel()
    content_button.setFixedSize(100, 100)
    content_button.setIcon(QIcon('content.jpg'))
    content_button.setIconSize(QSize(110, 110))
    content_button.clicked.connect(content_submit)
    content_layout.addWidget(content_button, alignment=Qt.AlignmentFlag.AlignCenter)
    content_layout.addWidget(content_label, alignment=Qt.AlignmentFlag.AlignCenter)

    '''Horizontal Layout Maximum of 4 Button'''
    section_layout1 = QHBoxLayout()
    section_layout1.setAlignment(Qt.AlignmentFlag.AlignLeft)
    section_layout1.addLayout(content_layout)

    '''Final Layout that Wraps all the Horizontal Layout'''
    section_wrapper = QVBoxLayout()
    section_wrapper.setAlignment(Qt.AlignmentFlag.AlignTop)
    section_label = QLabel('Section')
    section_wrapper.addWidget(section_label)
    section_wrapper.addLayout(section_layout1)

    '''Add Wrappers to Main QWidget()'''
    all_wrappers_layout.addLayout(section_wrapper)

代码取自一个大范围,其中包含上面的片段,一遍又一遍。我尝试了以下方法:


import os
import sys
import glob
import json
import datetime
from jsonmerge import merge, Merger
from PySide6.QtWidgets import QApplication, QWidget, QHBoxLayout, QPushButton, QLabel, QLineEdit, QVBoxLayout, QMainWindow, QScrollArea
from PySide6.QtGui import QIcon
from PySide6.QtCore import QSize
from PySide6.QtCore import Qt

location = f"./{str(datetime.datetime.now().strftime('%Y%m%d'))}.json"

class MainWindow(QWidget): 
    def __init__(self):
        super().__init__()
        self.wrappers = QVBoxLayout()

        with open('./bin/properties.json') as file:
            json_content = json.load(file)

        for element in list(json_content):
            self.layout_num = 0
            self.products_num = 1
            while not self.products_num == len(json_content[element]):
                self.product_layout = QHBoxLayout()
                self.product_layout.setAlignment(Qt.AlignmentFlag.AlignLeft)
                for _ in range(4):
                    self.btn_layout = QVBoxLayout()
                    self.btn_layout.setAlignment(Qt.AlignmentFlag.AlignLeft)
                    self.btn = QPushButton(self)
                    self.btn.setFixedSize(100, 100)
                    self.btn.setIcon(QIcon(json_content[element][self.products_num - 1]['icons']))
                    self.btn.setIconSize(QSize(110, 110))
                    self.texti = json_content[element][self.products_num - 1]['products']
                    self.btn.clicked.connect(self.templates)
                    self.btn_label = QLabel()
                    self.btn_layout.addWidget(self.btn_label, alignment=Qt.AlignmentFlag.AlignCenter)
                    self.btn_layout.addWidget(self.btn, alignment=Qt.AlignmentFlag.AlignCenter)
                    self.product_layout.addLayout(self.btn_layout)
                    if not self.products_num == len(json_content[element]):
                        self.products_num += 1
                self.wrappers.addLayout(self.product_layout)
                self.layout_num += 1
        self.setLayout(self.wrappers)

    def templates(self):
        if controllers['current_products'].get(self.texti) is not None:
            controllers['current_products'][self.texti] += 1
        else:
            controllers['current_products'][self.texti] = 1
        print(controllers)

def global_var():
    global controllers
    controllers = {'current_products': {}, 'current_prices': 0, 'customer_moneys': 0, 'current_expenses': 0, 'current_reasons': ''}

if __name__ == "__main__":
    if os.path.isfile(location) is False:
        with open(location, 'w') as f:
            f.write('{}')
    os.chdir(os.path.dirname(sys.argv[0]))
    global_var()

    app = QApplication()
    mainWin = MainWindow()
    mainWin.show()
    app.exec()

上面的唯一问题是:它向

controllers
添加了相同的项目,尽管按钮不同,图标也不同。我希望:
__templates__
被所有按钮使用,因此:只有 json 中的最后一项被
controllers
添加到
__templates__
Problems

我的期望是:

__templates__
将具有不同的属性取决于项目。换句话说:每个按钮都会通过
controllers['products']
__templates__

添加新项目

这是我的 JSON 配置:

{
    "section":
    [
        {
            "products": "product_name",
            "categories": "product_category",
            "icons": "img/section1/product1.jpg",
            "prices": 1
        }
    ],
    "section2":
    [
        {
            "products": "product_name",
            "categories": "product_category",
            "icons": "img/section2/product1.jpg",
            "prices": 1
        }
    ]
}

Generated Program

python-3.x pyqt pyside pyside6 pyqt6
© www.soinside.com 2019 - 2024. All rights reserved.