Python Forum
[PyQt] how to dynamically add label to pyqt window
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PyQt] how to dynamically add label to pyqt window
#1
I need to be able to dynamically add an object like a label to my window. How do I do that? Here is the code that I have.

import random
import sys

from PIL import Image
from PyQt5 import QtCore, QtWidgets
from PyQt5.QtGui import QPixmap
from PyQt5.QtWidgets import QApplication, QWidget
from Image2 import Ui_Form


class MyMainWindow(QWidget, Ui_Form):

    human_list = []

    def __init__(self, parent=None):
        super(MyMainWindow, self).__init__(parent)
        self.setupUi(self)
        self.addHuman(random.randint(1, 10000), random.randint(1, 500), random.randint(1, 200))

    def mouseDoubleClickEvent(self, event):
        self.addHuman(random.randint(1, 10000), random.randint(1, 500), random.randint(1, 200))

    def addHuman(self, humanId, x, y):
        human = HumanLabel(humanId, x, y, self)
        human.addHumanLabel()
        self.human_list.append(human)
        QApplication.processEvents()



class HumanLabel():

    def __init__(self, humanId, x, y, Form):
        self.humanId = humanId
        self.x = x
        self.y = y
        self.form = Form

    def addHumanLabel(self):
        self.label = QtWidgets.QLabel(self.form)
        self.label.setGeometry(QtCore.QRect(self.x, self.y, 500, 375))
        img = Image.open('./321.jpg')
        img.resize((20, 20), Image.ANTIALIAS)
        self.label.setPixmap(QPixmap('./456.jpg'))
        self.label.setObjectName(str(self.humanId))


if __name__ == '__main__':
    app = QApplication(sys.argv)
    win = MyMainWindow()
    win.show()
    sys.exit(app.exec_())
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtGui import QPixmap


class Ui_Form(object):
    def setupUi(self, Form):
        Form.setObjectName("Form")
        Form.resize(500, 375)
        self.label = QtWidgets.QLabel(Form)
        self.label.setGeometry(QtCore.QRect(0, 0, 500, 375))
        self.label.setPixmap(QPixmap('./123.jpg'))
        self.label.setObjectName("background")

        self.retranslateUi(Form)
        QtCore.QMetaObject.connectSlotsByName(Form)

    def retranslateUi(self, Form):
        _translate = QtCore.QCoreApplication.translate
        Form.setWindowTitle(_translate("Form", "Form"))
The problem is when I double click the mouse that the new human label is being created but it will not show on the window. How do I get the label to show on the window?
Reply


Messages In This Thread
how to dynamically add label to pyqt window - by Joongi - Aug-26-2019, 03:24 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Interaction between Matplotlib window, Python prompt and TKinter window NorbertMoussy 3 531 Mar-17-2024, 09:37 AM
Last Post: deanhystad
  PysimpleGUI window update dynamically SamLiu 6 4,058 Apr-05-2023, 02:32 PM
Last Post: SamLiu
  [Tkinter] Can't update label in new tk window, object has no attribute tompranks 3 3,565 Aug-30-2022, 08:44 AM
Last Post: tompranks
  [PyQt] Python PyQt5 - Change label text dynamically based on user Input ppel123 1 13,791 Mar-20-2020, 07:21 AM
Last Post: deanhystad
  tkinter window and turtle window error 1885 3 6,744 Nov-02-2019, 12:18 PM
Last Post: 1885
  A dynamically updating screen for PyQt GUI from URL bescf 0 2,641 Mar-25-2019, 06:58 AM
Last Post: bescf
  pyqt main window refresh poblems duende 0 5,384 Apr-13-2018, 05:05 PM
Last Post: duende
  [PyQt] how to dynamically add objects to pyqt window LavaCreeperKing 1 15,550 Apr-26-2017, 09:15 PM
Last Post: joe_anonimist
  update a variable in parent window after closing its toplevel window gray 5 9,106 Mar-20-2017, 10:35 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020