Python Forum
[PyQt] how to dynamically add objects to pyqt window
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PyQt] how to dynamically add objects 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.
class Main(QtGui.QMainWindow, stuff):
    def __init__(self):
        QtGui.QMainWindow.__init__(self)
        self.resize(600, 500)
        self.setup()
        
    def keyPressEvent(self, event):
        if event.key() == QtCore.Qt.Key_A:
            self.addblock()

    def addblock(self):
        self.newblock = QtGui.QLabel(self.frame)
        self.newblock.setGeometry(QtCore.QRect(10, 10, 40, 40))
        self.newblock.setPixmap(self.blockimg)#I use the label to display an image.
     
The problem is when I push the A key I know that the new label is being created but it will not show on the window. How do I get the label to show on the window?
Reply
#2
That depends on which layout manager you are using. There is a nice tutorial here

http://zetcode.com/gui/pyqt5/layout/
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Interaction between Matplotlib window, Python prompt and TKinter window NorbertMoussy 3 485 Mar-17-2024, 09:37 AM
Last Post: deanhystad
  PysimpleGUI window update dynamically SamLiu 6 4,000 Apr-05-2023, 02:32 PM
Last Post: SamLiu
  tkinter window and turtle window error 1885 3 6,712 Nov-02-2019, 12:18 PM
Last Post: 1885
  [PyQt] how to dynamically add label to pyqt window Joongi 3 10,639 Sep-10-2019, 01:38 PM
Last Post: Denni
  A dynamically updating screen for PyQt GUI from URL bescf 0 2,630 Mar-25-2019, 06:58 AM
Last Post: bescf
  pyqt main window refresh poblems duende 0 5,364 Apr-13-2018, 05:05 PM
Last Post: duende
  update a variable in parent window after closing its toplevel window gray 5 9,073 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