Close second widget after pressing save button pyqt5 - python-3.x

I am writing an application where main widget windows opens second widget window and in the second widget window, I am taking some inputs from user and on hitting save button, second widget window should saves the data into xml file and should get closed but the second window is not closing.
I tried most of the things from google like self.close(), self.destroy(),self.hide() self.window().hide(), self.window().destroy() none of them are working.
I don't want to do sys.exit() as this is closing complete application but just have to close secondWidgetWindow after clicking save button so that user can do another work in first widget window.
Below is the snippet :
FirstWidgetWindow.py
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_firstWidgetWindow(QtWidgets.QMainWindow):
def __init__(self,firstWidgetWindow):
super().__init__()
self.setupUi(firstWidgetWindow)
def setupUi(self, firstWidgetWindow):
### code to create Button ###
self.btnOpenNewWidgetWindow.clicked.connect(self.openNewWindow)
def openNewWindow(self):
self.secondWidgetWindow = QtWidgets.QWidget()
self.ui = Ui_secondWidgetWindow()
self.ui.setupUi(self.secondWidgetWindow)
self.secondWidgetWindow.show()
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
firstWidgetWindow = QtWidgets.QWidget()
ui = Ui_firstWidgetWindow(firstWidgetWindow)
firstWidgetWindow.show()
sys.exit(app.exec_())
secondWidgetWindow.py
class Ui_secondWidgetWindow(QtWidgets.QMainWindow):
def __init__(self):
super().__init__()
def setupUi(self, secondWidgetWindow):
### creating line edit to take input from user
### creating save button
self.btnSave.clicked.connect(self.saveUserInput)
def saveUserInput(self):
## saving user inputs in xml file
self.close() ## here i needs to close this window.

Close second widget after pressing save button:
self.secondWidgetWindow.hide()
Try it:
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_secondWidgetWindow(QtWidgets.QMainWindow):
def __init__(self):
super().__init__()
self.secondWidgetWindow = None
def setupUi(self, secondWidgetWindow):
self.secondWidgetWindow = secondWidgetWindow
### creating line edit to take input from user
self.line_edit = QtWidgets.QLineEdit(secondWidgetWindow)
self.line_edit.setGeometry(20, 20, 300, 20)
### creating save button
self.btnSave = QtWidgets.QPushButton('save', secondWidgetWindow)
self.btnSave.setGeometry(50, 50, 100, 50)
self.btnSave.clicked.connect(self.saveUserInput)
def saveUserInput(self):
## saving user inputs in xml file
#self.close() ## here i needs to close this window.
self.secondWidgetWindow.hide()
QtWidgets.QMessageBox.information(self, "SAVE",
"saving user inputs in xml file")
class Ui_firstWidgetWindow(QtWidgets.QMainWindow):
def __init__(self,firstWidgetWindow):
super().__init__()
self.setupUi(firstWidgetWindow)
def setupUi(self, firstWidgetWindow):
### code to create Button ###
self.btnOpenNewWidgetWindow = QtWidgets.QPushButton('OpenNewWidgetWindow', firstWidgetWindow)
self.btnOpenNewWidgetWindow.setGeometry(50, 100, 300, 50)
self.btnOpenNewWidgetWindow.clicked.connect(self.openNewWindow)
def openNewWindow(self):
self.secondWidgetWindow = QtWidgets.QWidget()
self.ui = Ui_secondWidgetWindow()
self.ui.setupUi(self.secondWidgetWindow)
self.secondWidgetWindow.show()
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
firstWidgetWindow = QtWidgets.QWidget()
ui = Ui_firstWidgetWindow(firstWidgetWindow)
firstWidgetWindow.setGeometry(700, 250, 400, 200)
firstWidgetWindow.show()
sys.exit(app.exec_())

Related

How to use QStackedWidget()

Am trying to use QStackedWidget() to switch to my next window but when i do that i get some errors that don't have when i run my ".py" files separately.
what my app should do is ... activate my group box with a click, then if i click the button, a new transparent window should pop-up with a mouse-cross listener, then when you click something it should stops returning the mouse cursor to normal and closing the transparent window.
window1.py
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtGui import QColor, QKeySequence, QIcon, QCursor
from window2 import *
class Ui_Form(QtWidgets.QWidget):
def __init__(self):
super(Ui_Form, self).__init__()
##if i use this method, it does not show my transparent window, also it shows an error that i dont get when run it separately.
def goToTransparentWindowMethod(self):
self.goToTransparentWindow = TransparentWindowClass()
myWindow.addWidget(self.goToTransparentWindow)
myWindow.setCurrentIndex(myWindow.currentIndex()+1)
def setupUi(self, myWindow):
myWindow.setObjectName("myWindow")
myWindow.resize(627, 327)
self.horizontalLayoutWidget = QtWidgets.QWidget(myWindow)
self.horizontalLayoutWidget.setGeometry(QtCore.QRect(10, 10, 300, 270))
self.horizontalLayoutWidget.setObjectName("horizontalLayoutWidget")
self.horizontalLayout = QtWidgets.QHBoxLayout(self.horizontalLayoutWidget)
self.horizontalLayout.setContentsMargins(0, 0, 0, 0)
self.horizontalLayout.setObjectName("horizontalLayout")
self.gpb_main = QtWidgets.QGroupBox(self.horizontalLayoutWidget)
self.gpb_main.setCheckable(True)
self.gpb_main.setChecked(False)
self.gpb_main.setObjectName("gpb_spell_main")
self.btn_main = QtWidgets.QPushButton(self.gpb_main)
self.btn_main.setGeometry(QtCore.QRect(10, 40, 88, 27))
self.btn_main.setObjectName("btn_main")
self.btn_main.clicked.connect(self.goToTransparentWindowMethod)
self.horizontalLayout.addWidget(self.gpb_main)
self.retranslateUi(myWindow)
QtCore.QMetaObject.connectSlotsByName(myWindow)
def retranslateUi(self, myWindow):
_translate = QtCore.QCoreApplication.translate
myWindow.setWindowTitle(_translate("myWindow", "Window1"))
self.gpb_main.setTitle(_translate("myWindow", "example"))
self.btn_main.setText(_translate("myWindow", "click me"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
myWindow = QtWidgets.QStackedWidget()
ui = Ui_Form()
ui.setupUi(myWindow)
myWindow.show()
sys.exit(app.exec_())
window2.py
from PyQt5 import QtCore, QtGui, QtWidgets
from pynput.mouse import Listener
import pyautogui
class TransparentWindowThreadClass(QtCore.QObject):
def __init__(self, parent=None):
super(TransparentWindowThreadClass, self).__init__()
#QtCore.pyqtSlot()
def on_click_main(self, x, y, button, pressed):
try:
if pressed:
self.pos_main = pyautogui.position()
self.get_rgb_main = pyautogui.pixel(self.pos_main[0], self.pos_main[1])
r = self.get_rgb_main.red
g = self.get_rgb_main.green
b = self.get_rgb_main.blue
self.get_rgb_main = r,g,b
Transparent_Window.unsetCursor()#error here when is called from window1
Transparent_Window.close()#error here when is called from window1
#Pressed
self.pressed_Msg = 'Pressed at x:{0} y:{1} RGB:{2}'.format(x, y, self.get_rgb_main)
print(self.pressed_Msg)
else:
#Released msg
self.released_Msg = 'Released at x:{0} y:{1} RGB:{2}'.format(x, y, self.get_rgb_main)
print(self.released_Msg)
if not pressed:
return False
except KeyboardInterrupt:
print('you pressed ctrl+c')
except NameError:
print("Error on_click_main")
except RuntimeError:
print('run time error')
except TypeError:
print('ype error')
except AttributeError:
print('Attribute Error')
#QtCore.pyqtSlot()
def loop_onclick_listener(self):
try:
with Listener(on_click=self.on_click_main) as listener:
listener.join()
except KeyboardInterrupt:
print('you pressed ctrl+c')
except NameError:
print("Error onclick_listener")
except RuntimeError:
print('run time error')
except TypeError:
print('ype error')
except AttributeError:
print('Attribute Error')
class TransparentWindowClass(QtWidgets.QWidget):
def __init__(self, parent=None):
super(TransparentWindowClass, self).__init__()
self.monitorResolution = pyautogui.size()
# create a QThread and start the thread that handles
self.thread = QtCore.QThread()
self.thread.start()
# create the worker without a parent so you can move it
self.worker = TransparentWindowThreadClass()## my custom thread class
# the worker is moved to another thread
self.worker.moveToThread(self.thread)
# if the thread started, connect it
self.thread.started.connect(self.worker.loop_onclick_listener)
def setupUi(self, Transparent_Window):
Transparent_Window.setObjectName("Transparent_Window")
Transparent_Window.resize(self.monitorResolution[0], self.monitorResolution[1])
Transparent_Window.setWindowFlags(QtCore.Qt.FramelessWindowHint)
Transparent_Window.setAttribute(QtCore.Qt.WA_TranslucentBackground, True)
Transparent_Window.setCursor(QtGui.QCursor(QtCore.Qt.CrossCursor))
self.retranslateUi(Transparent_Window)
QtCore.QMetaObject.connectSlotsByName(Transparent_Window)
def retranslateUi(self, Transparent_Window):
_translate = QtCore.QCoreApplication.translate
Transparent_Window.setWindowTitle(_translate("Transparent", "Transparent Window"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
Transparent_Window = QtWidgets.QWidget()
ui = TransparentWindowClass()
ui.setupUi(Transparent_Window)
Transparent_Window.show()
sys.exit(app.exec_())
You are making things too complex.
Also, even assuming that no other possible solution could be used, you must remember that widgets are not thread-safe, and can not be directly accessed from external threads. The only safe and correct way to communicate between threads is by using signals and slots.
That said, there is no need for pyautogui nor pynput if you just want to get the color of a pixel on the screen.
If you want to grab a pixel on the screen, you can use the QScreen function grabWindow(), using 0 as window id (which matches the whole screen), and with a single-pixel area.
Then you can use grabMouse() to ensure that you always receive mouse events, even if the mouse is outside of the widget and no mouse button was being pressed. Note that grabMouse() can only work on visible widgets, so we need to "hide" the window by moving it off-screen.
Then, overriding mousePressEvent() you can use grabWindow() with the global position of the mouse, it will return a QPixmap that can be converted to a QImage and get the pixelColor() of the grab above.
The grabbing can be canceled by pressing Esc.
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
class GrabTest(QWidget):
def __init__(self):
super().__init__()
self.resize(200, 200)
self.button = QPushButton('Grab!')
self.colorLabel = QLabel('No color', alignment=Qt.AlignCenter)
self.colorLabel.setFixedSize(120, 30)
self.colorLabel.setStyleSheet('border: 1px solid black;')
layout = QVBoxLayout(self)
layout.addStretch()
layout.addWidget(self.button, alignment=Qt.AlignCenter)
layout.addWidget(self.colorLabel, alignment=Qt.AlignCenter)
layout.addStretch()
self.button.clicked.connect(self.startGrab)
def mousePressEvent(self, event):
if QWidget.mouseGrabber() == self:
self.getPixel(event.globalPos())
else:
super().mousePressEvent(event)
def keyPressEvent(self, event):
if QWidget.mouseGrabber() == self and event.key() == Qt.Key_Escape:
self.stopGrab()
else:
super().keyPressEvent(event)
def startGrab(self):
self.grabMouse(Qt.CrossCursor)
self.oldPos = self.pos()
deskRect = QRect()
for screen in QApplication.screens():
deskRect |= screen.geometry()
# move the window off screen while keeping it visible
self.move(deskRect.bottomRight())
def stopGrab(self):
self.releaseMouse()
self.move(self.oldPos)
def getPixel(self, pos):
screen = QApplication.screens()[0]
pixmap = screen.grabWindow(0, pos.x(), pos.y(), 1, 1)
color = pixmap.toImage().pixelColor(0, 0)
if color.lightnessF() > .5:
textColor = 'black'
else:
textColor = 'white'
self.colorLabel.setStyleSheet('''
color: {};
background: {};
'''.format(textColor, color.name()))
self.colorLabel.setText(color.name())
self.stopGrab()
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
grabber = GrabTest()
grabber.show()
sys.exit(app.exec())

slot to right click menu action does does not work

I have written the below code to which I finally managed to add menu but connecitn menu to a function doesnt seem to work:
import os
from PyQt5 import uic
from PyQt5 import QtWidgets
from PyQt5 import QtCore
FILE_LOCATION = os.path.dirname(os.path.realpath(__file__))
class MainDialogWindow(QtWidgets.QDialog):
def __init__(self):
super(MainDialogWindow,self).__init__()
ui_file = os.path.join(FILE_LOCATION, "example.ui")
self._ui = uic.loadUi(ui_file, self)
self.registerCallbacks()
self.initUI()
def initUI(self):
"""Initialize the UI.
"""
self.textBrowser.setContextMenuPolicy(QtCore.Qt.CustomContextMenu)
def registerCallbacks(self):
self.textBrowser.customContextMenuRequested.connect(self.context_menu)
# self.connect(self.textBrowser, QtCore.Signal('customContextMenuRequested(const QPoint &)'), self.context_menu)
def context_menu(self, pos):
menu = QtWidgets.QMenu(self)
action = menu.addAction("clear")
menu.exec_(self.mapToGlobal(pos))
action.trigered.connect(self.clear)
def clear(self):
"""Slot to claer text.
"""
print("clear")
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
window = MainDialogWindow()
window.show()
window.setGeometry(500, 300, 300, 300)
sys.exit(app.exec_())
please helpp,, I want call the clear function from the right click menu
I don't seem to understand how the menu.exec_() method works, that method blocks the execution of sequential tasks until the user selects a QAction from the QMenu. In your case, for example, until when you press "clear" and the triggered signal is emitted (note: you have a typo), but at that moment there is no connection, so the clear method will not be called. The solution is to make the connection before invoking the QMenu exec_():
def context_menu(self, pos):
menu = QtWidgets.QMenu(self)
action = menu.addAction("clear")
action.triggered.connect(self.clear)
menu.exec_(self.mapToGlobal(pos))

Sending a signal from a button to a parent window

I'm making an app with PyQt5 that uses a FileDialog to get files from the user and saves the file names in a list. However I also want to be able to remove those entries after they are added, so I created a class that has a name(the file name) and a button. The idea is that when this button is clicked the widget disappears and the file entry is removed from the list. The disappearing part works fine but how to I get the widget to remove the entry form the list? How can i send a signal from one widget inside the window to the main app and tell it to remove the entry from the list?
I know the code is very bad, I'm still very new to PyQt and Python in general so any advice would be greatly appreciated.
from PyQt5 import QtWidgets as qw
import sys
class MainWindow(qw.QMainWindow):
def __init__(self):
super().__init__()
# List of opened files
self.files = []
# Main Window layout
self.layout = qw.QVBoxLayout()
self.file_display = qw.QStackedWidget()
self.file_button = qw.QPushButton('Add File')
self.file_button.clicked.connect(self.add_file)
self.layout.addWidget(self.file_display)
self.layout.addWidget(self.file_button)
self.setCentralWidget(qw.QWidget())
self.centralWidget().setLayout(self.layout)
# Open File Dialog and append file name to list
def add_file(self):
file_dialog = qw.QFileDialog()
self.files.append(file_dialog.getOpenFileName())
self.update_stack()
# Create new widget for StackedWidget remove the old one and display the new
def update_stack(self):
new_stack_item = qw.QWidget()
layout = qw.QVBoxLayout()
for file in self.files:
layout.addWidget(FileWidget(file[0]))
new_stack_item.setLayout(layout)
if len(self.file_display) > 0:
temp_widget = self.file_display.currentWidget()
self.file_display.removeWidget(temp_widget)
self.file_display.addWidget(new_stack_item)
class FileWidget(qw.QWidget):
def __init__(self, name):
# This widget is what is added when a new file is opened
# it has a file name and a close button
# my idea is that when the close button is pressed the widget is removed
# from the window and from the files[] list in the main class
super().__init__()
self.layout = qw.QHBoxLayout()
self.file_name = qw.QLabel(name)
self.close_button = qw.QPushButton()
self.close_button.clicked.connect(self.remove)
self.layout.addWidget(self.file_name)
self.layout.addWidget(self.close_button)
self.setLayout(self.layout)
def remove(self):
self.close()
if __name__ == '__main__':
app = qw.QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec_())
You need to remove from the list in the remove method of theFileWidget class.
import sys
from PyQt5 import QtWidgets as qw
class FileWidget(qw.QWidget):
def __init__(self, name, files): # + files
super().__init__()
self.files = files # +
self.name = name # +
self.layout = qw.QHBoxLayout()
self.file_name = qw.QLabel(name)
self.close_button = qw.QPushButton("close {}".format(name))
self.close_button.clicked.connect(self.remove)
self.layout.addWidget(self.file_name)
self.layout.addWidget(self.close_button)
self.setLayout(self.layout)
def remove(self):
self.files.pop(self.files.index(self.name)) # <<<-----<
self.close()
class MainWindow(qw.QMainWindow):
def __init__(self):
super().__init__()
# List of opened files
self.files = []
# Main Window layout
self.layout = qw.QVBoxLayout()
self.file_display = qw.QStackedWidget()
self.file_button = qw.QPushButton('Add File')
self.file_button.clicked.connect(self.add_file)
self.layout.addWidget(self.file_display)
self.layout.addWidget(self.file_button)
self.setCentralWidget(qw.QWidget())
self.centralWidget().setLayout(self.layout)
# Open File Dialog and append file name to list
def add_file(self):
file_name, _ = qw.QFileDialog().getOpenFileName(self, 'Open File') # +
if file_name: # +
self.files.append(file_name) # +
self.update_stack()
# Create new widget for StackedWidget remove the old one and display the new
def update_stack(self):
new_stack_item = qw.QWidget()
layout = qw.QVBoxLayout()
for file in self.files:
layout.addWidget(FileWidget(file, self.files)) # + self.files
new_stack_item.setLayout(layout)
if len(self.file_display) > 0:
temp_widget = self.file_display.currentWidget()
self.file_display.removeWidget(temp_widget)
self.file_display.addWidget(new_stack_item)
if __name__ == '__main__':
app = qw.QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec_())

PyQt5 - How to draw a dot on mouse click position?

I am trying to draw a dot on my main window, but the dot is not shown.
I've tried bounding mousePressEvent to paintEvent, but it didn't work as well. Here's current version of my code(which is not working too). Also I tried place a point with drawPoint method and it didn't work too.
import sys
from PyQt5 import QtWidgets, QtGui, QtCore, uic
class GUI(QtWidgets.QMainWindow):
def __init__(self):
super().__init__()
uic.loadUi('gui.ui', self)
self.setFixedSize(self.size())
self.show()
def mousePressEvent(self, e):
print(e.pos())
qp = QtGui.QPainter()
qp.begin(self)
qp.setPen(QtCore.Qt.red)
qp.drawEllipse(e.pos().x(), e.pos().y(), 10, 10)
qp.end()
self.update()
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
window = GUI()
sys.exit(app.exec_())
I know that mousePressEvent is working since I get coords of the click.
I am okay to change methods of dot-placing or type of dots to place, but it should have customizable color and size.
You should only draw within the paintEvent method, and this paint does not save memory so if you want to graph several points you must store them in some container, for example using QPolygon.
paintEvent() is called every time you call update() or repaint(), for example it is called every time it is resized, the window is moved, etc.
import sys
from PyQt5 import QtWidgets, QtGui, QtCore, uic
class GUI(QtWidgets.QMainWindow):
def __init__(self):
super().__init__()
uic.loadUi('gui.ui', self)
self.setFixedSize(self.size())
self.show()
self.points = QtGui.QPolygon()
def mousePressEvent(self, e):
self.points << e.pos()
self.update()
def paintEvent(self, ev):
qp = QtGui.QPainter(self)
qp.setRenderHint(QtGui.QPainter.Antialiasing)
pen = QtGui.QPen(QtCore.Qt.red, 5)
brush = QtGui.QBrush(QtCore.Qt.red)
qp.setPen(pen)
qp.setBrush(brush)
for i in range(self.points.count()):
qp.drawEllipse(self.points.point(i), 5, 5)
# or
# qp.drawPoints(self.points)
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
window = GUI()
sys.exit(app.exec_())

Pyqt5 method to close or delete the widget and setup it again on command

I have created short application with Pyqt5 designer:
Here is the code:
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(752, 674)
self.formLayout_2 = QtWidgets.QFormLayout(Form)
self.formLayout_2.setObjectName("formLayout_2")
self.formLayout = QtWidgets.QFormLayout()
self.formLayout.setObjectName("formLayout")
self.formLayout_2.setLayout(0, QtWidgets.QFormLayout.LabelRole, self.formLayout)
self.pushButton = QtWidgets.QPushButton(Form)
self.pushButton.setObjectName("pushButton")
self.formLayout_2.setWidget(0, QtWidgets.QFormLayout.FieldRole, self.pushButton)
self.widget = QtWidgets.QWidget(Form)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Preferred)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(1)
sizePolicy.setHeightForWidth(self.widget.sizePolicy().hasHeightForWidth())
self.widget.setSizePolicy(sizePolicy)
self.widget.setStyleSheet("background-color:white;")
self.widget.setObjectName("widget")
self.formLayout_2.setWidget(2, QtWidgets.QFormLayout.FieldRole, self.widget)
self.pushButton_2 = QtWidgets.QPushButton(Form)
self.pushButton_2.setObjectName("pushButton_2")
self.formLayout_2.setWidget(1, QtWidgets.QFormLayout.FieldRole, self.pushButton_2)
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "Form"))
self.pushButton.setText(_translate("Form", "Letters"))
self.pushButton_2.setText(_translate("Form", "Numbers"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
Form = QtWidgets.QWidget()
ui = Ui_Form()
ui.setupUi(Form)
Form.show()
sys.exit(app.exec_())
This script is connected to appexe script(dialog):
import sys
from PyQt5 import QtWidgets, QtCore, QtGui
import string
from functools import partial
from setup_test import Ui_Form
from PyQt5.QtWidgets import QWidget, QGridLayout, QPushButton, QApplication
class myprog(Ui_Form):
def __init__ (self, dialog):
Ui_Form.__init__(self)
self.setupUi(dialog)
self.symbols = [string.ascii_lowercase, string.digits]
self.buttons = [self.pushButton, self.pushButton_2]
for i,v in enumerate(self.buttons):
self.buttons[i].clicked.connect(partial(self.application, i))
def application(self, i):
self.grid1 = QGridLayout(self.widget)
names = [x for x in self.symbols[i]]
positions = [(j,d) for j in range(7) for d in range(6)]
for a,b in zip(positions, names):
button = QPushButton(b)
self.grid1.addWidget(button, *a)
print(self.grid1.count())
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
dialog = QtWidgets.QDialog()
appexe = myprog(dialog)
dialog.show()
sys.exit(app.exec_())
I am new to Pyqt5. The basic idea is: whenever you click the button 'Letters' or button 'Numbers', the widget below should layout all the sysmbols within Letters or buttons.
If you do execute the appexe script, you can see, it works for the first try. (Please ignore the visual design). I am happy with the result. When button is pressed, it shows the buttons in grid(widget).
The problem I am facing is, how to clear the grid and it's widget when other button is hit > and display the new grid, new widgets.
If you look close, there is also a print statement:
print(self.grid1.count())
When I am in application and i am clicking letter and number button, it tells me how many widgets are within grid, however I just can't find a way how to display the new setup.
I would appreciate your help and your ideas. Thank you.
There are a number of different ways to remove a layout and its child widgets. However, the only one that seems to work properly with your particular example, is this one:
def application(self, i):
layout = self.widget.layout()
if layout is not None:
QWidget().setLayout(layout)
...
This works because setting the layout on a different widget will automatically re-parent that layout and all its children. Then when the temporary parent widget is immediately garbage-collected, Qt will automatically delete all its children as well.

Resources