1

I would like to create a simple GUI that assign to each name another name randomly. But the problem is that, after the user decide how many name create, it should show the QLineEdit tag, but i can't see that. There aren't errors. Obviusly,the application is not over. The problem is only the QLineEdit tag. Here there is the code:

from PyQt5.QtWidgets import *
from PyQt5.QtCore import Qt
from PyQt5 import QtGui
import sys

class Assigner(QWidget):
    def __init__(self):
        super().__init__()
        self.setGeometry(0,0,250,500)
        self.setWindowTitle("Assigner")
        self.counter = 3

        self.tit = QLabel(self)
        self.tit.setText("Assigner GUI")
        self.tit.move (60,10)
        self.tit.setFont(QtGui.QFont("Times", 20, QtGui.QFont.Bold))

        self.butup = QPushButton("↑",self)
        self.butup.resize(40,40)
        self.butup.setToolTip("Increase number")
        self.butup.move(100,50)
        self.butup.clicked.connect(self.increase)

        self.lab = QLabel(self)
        self.lab.setText(str(self.counter))
        self.lab.resize(40,60)
        self.lab.move (100,115)
        self.lab.setStyleSheet("background: red;")
        self.lab.setFrameShape(QFrame.Panel)
        self.lab.setFrameShadow(QFrame.Sunken)
        self.lab.setLineWidth(4)
        self.lab.setFont(QtGui.QFont("Times", 20, QtGui.QFont.Bold))
        self.butdo = QPushButton("↓",self)
        self.butdo.resize(40,40)
        self.butdo.setToolTip("Decrease number")
        self.butdo.move(100,200)
        self.butdo.clicked.connect(self.decrease)

        self.go = QPushButton("Start assign",self)
        self.go.resize(70,40)
        self.go.setToolTip("Start")
        self.go.move(85,280)
        self.go.clicked.connect(self.start)
        self.show()

    def increase(self):
        self.counter += 1
        self.lab.setText(str(self.counter))
    def decrease(self):
        if self.counter > 0:
            self.counter -= 1
            self.lab.setText(str(self.counter))

    def start(self):
        self.go.deleteLater()
        self.butdo.deleteLater()
        self.butup.deleteLater()
        self.lab.deleteLater()
        self.tit.deleteLater()
        self.entry = []
        self.y = 20
        for i in range(self.counter):
            self.entry.append(QLineEdit(self))
            self.entry[-1].move(20, self.y)
            self.entry[-1].resize(220,40)
            self.y += 50

if __name__ == "__main__":
    app = QApplication(sys.argv)
    ass = Assigner()
    sys.exit(app.exec_())

When you click the start button, all is white, but we should see the QLineEdit. If someone knows where is the issue, please write it to me

Matteo Possamai
  • 455
  • 1
  • 10
  • 23

1 Answers1

2

Your QLineEdits are simply invisible; just add:

self.entry[-1].show()
# or self.entry[-1].setVisible(True)

There's a (discrete) note about that in Qwidget's doc:

If you add a child widget to an already visible widget you must explicitly show the child to make it visible.

So an alternative solution could be to self.hide() at the very beginning of start, modify your sub-widgets as before, and finish the function with a self.show().

Demi-Lune
  • 1,868
  • 2
  • 15
  • 26