如何使用pyqt4找出可点击的小部件索引

时间:2019-01-27 04:04:33

标签: python python-2.7 pyqt pyqt4

在我的程序中,我需要创建多个窗口小部件订单,因此我通过使用字典列表来创建窗口小部件列表。我对可单击窗口小部件使用了鼠标释放事件,但没有得到哪个窗口小部件的对象发件人已点击。请问有谁能帮助我找到被点击的小部件索引。 下面是我的代码:

from datetime import datetime
from PyQt4 import QtCore, QtGui
from functools import partial
class Orders(QtGui.QMainWindow):
    def __init__(self, parent=None):
        super(Orders, self).__init__(parent)
        Online_order_dict = [{"Date":"jan-24-2019","Id":"#175","Amount":"191 rs", "Shopping":"Online","Order_City":"Hyderbad","Order_State":"TELANGANA"},{"Date":"jan-25-2019","Id":"#186","Amount": "200 Rs", "Shopping":"Online","Order_City":"Hyderbad","Order_State":"TELANGANA"},{"Date":"jan-29-2019","Id":"#188","Amount": "250 Rs", "Shopping":"Online","Order_City":"Hyderbad","Order_State":"TELANGANA"},{"Date":"jan-25-2019","Id":"#176","Amount": "200 Rs", "Shopping":"Online",
        "Order_City":"Hyderbad","Order_State":"TELANGANA"},{"Date":"jan-28-2019","Id":"#201","Amount": "250 Rs", "Shopping":"Online","Order_City":"Hyderbad","Order_State":"TELANGANA"}]

        self.qvw1 = QtGui.QWidget()
        self.scrollArea = QtGui.QScrollArea(widgetResizable=True)
        self.scrollArea.setWidget(self.qvw1)

        self.online_order_hbox = QtGui.QVBoxLayout(self.qvw1)
        self.online_label = QtGui.QPushButton("Online Order")
        self.online_label.setStyleSheet("QPushButton{ background-color: #FF8C00; color: white;outline : None;}")
        self.online_order_hbox.addWidget(self.online_label,QtCore.Qt.AlignTop)
        self.qvw1.setFixedWidth(round((550)))
        for i in Online_order_dict:
            self.w1 = QtGui.QWidget()
            # self.w1.mouseReleaseEvent=self.myfunction1
            self.w1_vbox=QtGui.QVBoxLayout(self.w1)
            self.hline1 = QtGui.QFrame();self.hline1.setFrameShape(QtGui.QFrame.HLine)
            self.date_label1 = QtGui.QLabel(i["Date"]  + "   " + i["Id"], alignment= QtCore.Qt.AlignCenter,objectName="small")
            self.amount = QtGui.QLabel("Amount:" + i["Amount"])
            self.shopping = QtGui.QLabel("Shopping :" + i["Shopping"])
            self.ordercity = QtGui.QLabel("Order City :" + i["Order_City"])
            self.orderstate = QtGui.QLabel("Order State :" +i["Order_State"])
            self.w1_vbox.addWidget(self.date_label1)
            self.w1_vbox.addWidget(self.amount)
            self.w1_vbox.addWidget(self.shopping)
            self.w1_vbox.addWidget(self.ordercity)
            self.w1_vbox.addWidget(self.orderstate)
            self.w1_vbox.addWidget(self.hline1)
            self.online_order_hbox.addWidget(self.w1)

        self.setCentralWidget(self.scrollArea)
if __name__ == '__main__':
    import sys


    app = QtGui.QApplication(sys.argv)

    settingobj= Orders()
    settingobj.show()
    settingobj.showFullScreen()
    sys.exit(app.exec_())

1 个答案:

答案 0 :(得分:2)

虽然应用程序的复杂性增加了,但是最好使用专门化策略,即创建一个仅负责的类,在这种情况下,创建一个类,当用鼠标按下该类时会发出信号,并且类必须具有样本信息才能轻松访问。

min_kids = bank_df["children"].min()
max_kids = bank_df["children"].max()
print (min_kids, max_kids)
plt.hist(bank_df["children"], bins=4, align='mid')
plt.xlabel('Children')
plt.ylabel('Count')
plt.title('Histogram of Number of Customers Children')
plt.axis([0, 3, 0, 300])