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
#3
(Aug-26-2019, 03:25 PM)Denni Wrote: I think your biggest issue was that you were not showing the Label which means while you were creating it you could not see it. Still there might have been a few other issues with how you were going about it so I have tweaked what you had and made it more Classy [which is to say I expounded on the class ;)]. Now other than not showing images because I did not have them this code does work and using the coordinates as text. Note I capped your random coordinates so that the text remained on the form you will have to do something similar when you use the images (basically subtract the image size from your maximum values to get your maximum coordinates).

from random import randint
from sys import exit as sysExit
 
#from PIL import Image

from PyQt5.QtGui import QPixmap

from PyQt5.QtWidgets import QApplication, QWidget
from PyQt5.QtWidgets import QLabel

# Okay I was not sure what this was or why but it did not
# seem to me to be part of adding a human to the form and
# since I could not use it anyway I removed it to here
#    img = Image.open('./321.jpg')
#    img.resize((20, 20), Image.ANTIALIAS)


class HumanLabel(QLabel):
    def __init__(self, parent):
        QLabel.__init__ (self, parent)

        self.HumanLocale = parent

    @property
    def HumanLocale(self):
      # On what Form is the Human located
        return self.__parent

    @HumanLocale.setter
    def HumanLocale(self, value):
        self.__parent = value

    @property
    def xCor(self):
      # Human's current X-Coordinate
        return self.__xCoordinate

    @xCor.setter
    def xCor(self, value):
        self.__xCoordinate = value

    @property
    def yCor(self):
      # Human's current Y-Coordinate
        return self.__yCoordinate

    @yCor.setter
    def yCor(self, value):
        self.__yCoordinate = value

    @property
    def HumanImage(self):
      # Human's current Image
        return self.__HumanImage

    @HumanImage.setter
    def HumanImage(self, value):
        self.__HumanImage.setPixmap(QPixmap(value))

    def placeHuman(self):
        self.move(self.xCor, self.yCor)
      # I do not have access to your Image so did this instead
        value = str(self.xCor) + '-' + str(self.yCor)
        self.setText(value)
#        self.Humans[humanId].HumanImage = './456.jpg'
        self.show()

    def addHumanRandomly(self):
      # Note because you do not currently check to see if
      # the current humanId is not already in your list
      # you may end up just moving them instead of creating
      # them as such I would suggest such a check here and
      # creating another method called moveHuman
        self.xCor = randint(1, 450)
        self.yCor = randint(1, 350)
        self.placeHuman()
        
class MyMainWindow(QWidget):
    def __init__(self):
        super(MyMainWindow, self).__init__()

        self.Humans = {}
 
        self.setupUi()

        humanId = randint(1, 10000)
        self.Humans[humanId] = HumanLabel(self)
        self.Humans[humanId].addHumanRandomly()

    def setupUi(self):
        self.setWindowTitle('Form')
        self.resize(500, 375)

        humanId = 1
        self.Humans[humanId] = HumanLabel(self)
        self.Humans[humanId].xCor = 0
        self.Humans[humanId].yCor = 0
#        self.Humans[humanId].HumanImage = './123.jpg'
        self.Humans[humanId].placeHuman()
 
    def mouseDoubleClickEvent(self, event):
        humanId = randint(1, 10000)
      # Create the object within the Dictionary
        self.Humans[humanId] = HumanLabel(self)
      # Then using that object stored in our Dictionary
      # we set its coordinates and show it
        self.Humans[humanId].addHumanRandomly()
 
if __name__ == '__main__':
    MainThred = QApplication([])

    MainGui = MyMainWindow()
    MainGui.show()

    sysExit(MainThred.exec_())
As you can see I used a dictionary to house your dynamic creation of labels it is the only way I know that you can do that but there might be other methods. Now if you wanted to track your Human Ids for any reason I would create a separate list for those or just reference the .keys() value of the dictionary. Of course if you have any questions about why I did any of the things I did just ask.

Big Grin Thanks,I solved my proble, I found I should add 'label.show()' every time I create a label. And thanks for Ur suggestions
Reply


Messages In This Thread
RE: how to dynamically add label to pyqt window - by Joongi - Sep-09-2019, 02:09 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Interaction between Matplotlib window, Python prompt and TKinter window NorbertMoussy 3 608 Mar-17-2024, 09:37 AM
Last Post: deanhystad
  PysimpleGUI window update dynamically SamLiu 6 4,101 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,609 Aug-30-2022, 08:44 AM
Last Post: tompranks
  [PyQt] Python PyQt5 - Change label text dynamically based on user Input ppel123 1 13,826 Mar-20-2020, 07:21 AM
Last Post: deanhystad
  tkinter window and turtle window error 1885 3 6,793 Nov-02-2019, 12:18 PM
Last Post: 1885
  A dynamically updating screen for PyQt GUI from URL bescf 0 2,654 Mar-25-2019, 06:58 AM
Last Post: bescf
  pyqt main window refresh poblems duende 0 5,405 Apr-13-2018, 05:05 PM
Last Post: duende
  [PyQt] how to dynamically add objects to pyqt window LavaCreeperKing 1 15,575 Apr-26-2017, 09:15 PM
Last Post: joe_anonimist
  update a variable in parent window after closing its toplevel window gray 5 9,172 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