Python Forum
How to add a widget to a QtWindow after it has been created
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to add a widget to a QtWindow after it has been created
#1
I think I asked this before, but I was unable to get an answer. I need to know how I can add widgets to my window after it has been created.
rom PyQt5 import QtCore, QtGui, QtWidgets
import sys, random

class window(QtWidgets.QMainWindow):
    def setup(self):
        self.resize(300,300)
        self.setMaximumSize(300, 300)
        self.setMinimumSize(300, 300)

        self.button = QtWidgets.QPushButton(self)
        self.button.setGeometry(QtCore.QRect(50, 80, 121, 41))
        self.button.clicked.connectself.addB)

    def addB(self):
        y = random.randint(2, 200)
        b = QtWidgets.QPushButton(self)
        b.setGeometry(QtCore.QRect(50, y, 121, 41))
The code here will create the first button when I call window.setup() and I see it when I call window.show() to make the window show, but I can't figure out how to add objects to the window after that. What I would like to do is create buttons, and add them to a list. That way I can save them, and load them again when I restart the program.
Reply


Messages In This Thread
How to add a widget to a QtWindow after it has been created - by LavaCreeperKing - Jul-02-2019, 05:01 PM

Forum Jump:

User Panel Messages

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