在另一个QWidget类的函数中启动自定义QWidget

2024-10-02 18:15:15 发布

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

我不熟悉python和pyqt。我正在尝试创建一个QToolButtons矩阵,按下按钮后,弹出一个QDialog供用户输入(不止一个字段)

我有一个用于button matrix对象的类和一个用于dialog的类,但似乎无法在button matrix类中获得函数来初始化dialog类/小部件的实例

谁能告诉我我做错了什么

我已提供以下代码:

from PyQt4 import QtGui
from PyQt4.QtGui import QApplication, QWidget, QFormLayout, QInputDialog, QPushButton, QToolButton, QLabel, QVBoxLayout, QHBoxLayout, QLineEdit

class Pixel(object):
    def __init__(self, pixel_number, r_value, g_value, b_value):
        self.pixel = pixel_number
        self.red_value = r_value
        self.green_value = g_value
        self.blue_value = b_value
        
class inputdialogdemo(QWidget):
    
    def __init__(self, parent = None):
        QWidget.__init__(self, parent)
        #super(inputdialogdemo, self).__init__(parent)
        
        layout = QFormLayout()
        self.btn1 = QPushButton("Enter red value")
        self.le1 = QLineEdit()
        self.btn1.clicked.connect(self.getRed)
        layout.addRow(self.btn1,self.le1)
    
        self.btn2= QPushButton("Enter green value")
        self.le2 = QLineEdit()
        self.btn2.clicked.connect(self.getGreen)
        layout.addRow(self.btn1,self.le2)
      
        self.btn3 = QPushButton("Enter blue value")
        self.le3 = QLineEdit()
        self.btn3.clicked.connect(self.getBlue)
        layout.addRow(self.btn3,self.le3)
      
        self.setLayout(layout)
        self.setWindowTitle("RGB input dialog ")
        
    def getRed(self):
        num, ok = QInputDialog.getText(self, 'Red Input Dialog', 'Enter your name:')
        
        if ok:
            self.le1.setText(str(num))
            
    def getGreen(self):
        num,ok = QInputDialog.getInt(self,"Green input dualog","enter a number")
        
        if ok:
            self.le2.setText(str(num))  
    
    def getBlue(self):
        num,ok = QInputDialog.getInt(self,"Blue input dualog","enter a number")
        
        if ok:
            self.le3.setText(str(num))    


class ClusterArray(QWidget):
    def __init__(self, parent=None):
        QWidget.__init__(self, parent)
        #self.button_layout = QHBoxLayout()
        self.button_layout = QtGui.QGridLayout()
        self.widget_layout = QtGui.QGridLayout()
        for cluster_number in range(1, 15):
            for pixel_number in range(1, 5):
                    button = QToolButton()
                    button.setText(str(cluster_number) + str(pixel_number))
                    button.setObjectName(f"Cluster{cluster_number},Pixel{pixel_number}")
                    button.released.connect(self.button_released)
                    self.button_layout.addWidget(button, cluster_number, pixel_number)

        self.status_label = QLabel('No button clicked')

        self.widget_layout.addItem(self.button_layout)
        self.widget_layout.addWidget(self.status_label)
        self.setLayout(self.widget_layout)
        ex = inputdialogdemo()
        
    def button_released(self):
        sending_button = self.sender()
        self.status_label.setText('%s Clicked!' % str(sending_button.objectName()))
        ex = inputdialogdemo()
        ex.show()
        #i, okPressed = QInputDialog.getInt(self, "Get integer","Percentage:", 28, 0, 100, 1)
        
if __name__ == '__main__':
    app = QApplication(sys.argv)
    widget = ClusterArray() 
    widget.show()
#   ex = inputdialogdemo()
#   ex.show()
    sys.exit(app.exec_()

目前,我刚刚尝试让我的类函数从一个名为“inputdialogdemo”的类中的一些演示代码创建一个对象

此外,我希望以某种方式保留与矩阵上相应按钮关联的输入值。我认为这些值应该存储在每个按钮所代表的另一个对象中。我的对象是“像素”,它包含红色、绿色和蓝色的值。我还没有编写这个功能。这个方法听起来可行吗


Tags: selfnumberinitvaluedefbuttonokwidget
1条回答
网友
1楼 · 发布于 2024-10-02 18:15:15

__init__button_released中都正确地创建了inputdialogdemo实例,问题是一旦这些函数返回,ex实例就得到垃圾回收:因为没有持久引用(ex只是一个局部变量),python会自动删除它,以避免不必要的内存消耗

因为您需要一个对话框,所以最好的解决方案是从QDialog而不是QWidget继承;这有两个重要的好处:它保持对话框模态(它位于其他窗口的顶部,避免与它们交互),并提供^{}方法,该方法在对话框关闭之前不会返回;然后,您可以为标准的Ok/Cancel按钮添加QDialogButtonBox,并将其^{}^{}连接到对话框的^{}^{}插槽

from PyQt4.QtGui import (QApplication, QWidget, QFormLayout, QInputDialog, QPushButton, QToolButton, 
    QLabel, QVBoxLayout, QHBoxLayout, QLineEdit, QDialog, QDialogButtonBox)

class inputdialogdemo(QDialog):
    def __init__(self, parent = None):
        # ...
        buttonBox = QDialogButtonBox(QDialogButtonBox.Ok|QDialogButtonBox.Cancel)
        layout.addRow(buttonBox)
        buttonBox.accepted.connect(self.accept)
        buttonBox.rejected.connect(self.reject)

class ClusterArray(QWidget):
    # ...
    def button_released(self):
        sending_button = self.sender()
        self.status_label.setText('%s Clicked!' % str(sending_button.objectName()))
        ex = inputdialogdemo()
        if ex.exec_():
            red = ex.le1.text()
            green = ex.le2.text()
            blue = ex.le2.text()

一些建议:

  • 如果您需要一个数值,不要使用QLineEdit,而是使用QSpinBox
  • 要向另一个布局添加布局,请使用setLayout(),而不是addItem()
  • 要连接到按钮单击,请使用clicked信号,而不是released
  • 虽然对象名称中不严格禁止使用特殊字符,但通常最好避免使用它们;另外,使用实际对象名称的对象名称,而不是跟踪某些属性
  • 您可以使用dynamic properties跟踪任何QObject的自定义属性:
    button.setProperty('cluster', cluster_number)
    button.setProperty('pixel', pixel_number)
    <> LI>除非您有强制性的系统要求,否则您应该真正考虑切换到PYQT5,因为PYQT4自2015以来被认为是过时的和不受欢迎的/不支持的;李>
  • 对于类,始终首选大写名称,因为小写名称通常仅用于变量和属性;阅读更多关于 Style Guide for Python Code

相关问题 更多 >