我不熟悉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”的类中的一些演示代码创建一个对象
此外,我希望以某种方式保留与矩阵上相应按钮关联的输入值。我认为这些值应该存储在每个按钮所代表的另一个对象中。我的对象是“像素”,它包含红色、绿色和蓝色的值。我还没有编写这个功能。这个方法听起来可行吗
在
__init__
和button_released
中都正确地创建了inputdialogdemo
实例,问题是一旦这些函数返回,ex
实例就得到垃圾回收:因为没有持久引用(ex
只是一个局部变量),python会自动删除它,以避免不必要的内存消耗因为您需要一个对话框,所以最好的解决方案是从} 方法,该方法在对话框关闭之前不会返回;然后,您可以为标准的Ok/Cancel按钮添加QDialogButtonBox,并将其^{} 和^{} 连接到对话框的^{} 和^{} 插槽
QDialog
而不是QWidget
继承;这有两个重要的好处:它保持对话框模态(它位于其他窗口的顶部,避免与它们交互),并提供^{一些建议:
setLayout()
,而不是addItem()
李>clicked
信号,而不是released
李><> LI>除非您有强制性的系统要求,否则您应该真正考虑切换到PYQT5,因为PYQT4自2015以来被认为是过时的和不受欢迎的/不支持的;李>- 对于类,始终首选大写名称,因为小写名称通常仅用于变量和属性;阅读更多关于
Style Guide for Python Code李>
相关问题 更多 >
编程相关推荐