Results 1 to 2 of 2

Thread: (PyQt4) How to update a label with a random value from a list

  1. #1
    Join Date
    Feb 2017
    Posts
    1
    Qt products
    Platforms
    Windows

    Default (PyQt4) How to update a label with a random value from a list

    Basically, I'm making a program that relies on random generation from lists. The first generation works fine but I want it so that when I press the "random" button, the field is automatically updated with another name. I'm new to PyQt (a few days) and Python in general so a little help would be appreciated


    Qt Code:
    1. def random_hero(self):
    2. hero_dict = ["Adagio", "Alpha", "Ardan", "Baron", "Blackfeather", "Catherine", "Celeste", "Flicker", \
    3. "Fortress", "Glaive", "Gwen", "Idris", "Joule", "Kestrel", "Koshka", "Krul", "Lance", "Lyra", "Ozo", "Petal", \
    4. "Phinn", "Reim", "Ringo", "Rona", "Samuel", "Saw", "Skaarf", "Skye", "Taka", "Vox"]
    5. num = 1
    6. newRandomHero = random.sample(hero_dict,num)
    7. global randHeroName
    8. randHeroName = newRandomHero[0]
    9. print (randHeroName)
    10. heroName = randHeroName
    11.  
    12. def tab3UI(self):
    13. self.setGeometry(250, 100, 800, 300)
    14. layout = QGridLayout()
    15. hero_dict = ["Adagio", "Alpha", "Ardan", "Baron", "Blackfeather", "Catherine", "Celeste", "Flicker", \
    16. "Fortress", "Glaive", "Gwen", "Idris", "Joule", "Kestrel", "Koshka", "Krul", "Lance", "Lyra", "Ozo", "Petal", \
    17. "Phinn", "Reim", "Ringo", "Rona", "Samuel", "Saw", "Skaarf", "Skye", "Taka", "Vox"]
    18. num = 1
    19. randomHero = random.sample(hero_dict,num)
    20.  
    21. layout.addWidget(QLabel("Hero:"),0,0)
    22. global heroName
    23. heroName = layout.addWidget(QLabel(randHeroName),0,1)
    24.  
    25. self.btnRandomHero = QPushButton("Random")
    26. layout.addWidget(self.btnRandomHero,0,3)
    27. self.btnRandomHero.clicked.connect(self.random_hero)
    28.  
    29. blankSpaceLabel1 = layout.addWidget(QLabel(""),0,4,0,8)
    30. blankSpaceLabel1 = layout.addWidget(QLabel(""),1,0,0,8)
    31.  
    32. self.setTabText(2,"Ultimate Bravery")
    33. self.tab3.setLayout(layout)
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: (PyQt4) How to update a label with a random value from a list

    If you need to access an object from multiple places you need to store it in a variable that is accessible from all those places.

    E.g. a member variable in a class or a global variable.

    In your case you need to store the QLabel object that you want to use for displaying the hero name.
    Then, in your random_hero function, you can write the new name to its "text" property or call its "setText()" function.

    Cheers,
    _

Similar Threads

  1. Replies: 4
    Last Post: 27th May 2015, 15:49
  2. PYQT4 - Trying to pass a list to QProcess
    By cminogue15 in forum Newbie
    Replies: 1
    Last Post: 24th January 2015, 00:33
  3. Combit List & Label with QT Software
    By jijoadoor71 in forum Qt Programming
    Replies: 0
    Last Post: 26th June 2014, 07:24
  4. unable to update label from another thread
    By dhanya.v in forum Qt Programming
    Replies: 1
    Last Post: 4th October 2012, 13:05
  5. ui label list not updating
    By The physicist in forum Newbie
    Replies: 4
    Last Post: 21st December 2010, 12:41

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.