Recently, I started programming in python, right now I'm writing a GUI for a password and pseudo-random number generator. I had this problem: TypeError: 'PySide6.QtWidgets.QLineEdit' object is not iterable
btn_2 = QPushButton("Generate password", self)
btn_2.resize(100,30)
btn_2.move(340, 250)
btn_2.resize(100, 30)
btn_2.clicked.connect(self.btn1)
btn_3 = QPushButton("Generate pseudorandom numbers", self)
btn_3.move(140, 250)
btn_3.resize(180, 30)
btn_3.clicked.connect(self.btn2)
self.line = QLineEdit(self)
self.line.setPlaceholderText("Enter the expected string length")
self.line.resize(250, 20)
self.line.move(200, 220)
self.onlyInt = QIntValidator()
self.line.setValidator(self.onlyInt)
self.show()
def btn1(self):
mark = self.line
chars = string.ascii_letters string.digits
print("".join(random.choice(chars) for _ in mark))
def btn2(self):
mark = self.line
chars = string.digits
print("".join(random.choice(chars) for _ in mark))
CodePudding user response:
use self.line.text
rather than self.line
.
So:
mark = int(self.line.text)
chars = string.ascii_letters string.digits
print("".join(random.choice(chars) for _ in range(mark))
Note that I've also replaced mark
with range(mark)
which is probably what you want here.
In general when given an XYZ isn't iterable error, go and look at the docs for XYZ and see how to get at whatever it is storing and you want to iterate over. In this case we see that we can get at the text which is presumably what you want with .text
. But as it happens I think you want to iterate self.line.text
times (hence your checking for an int), so we cast to int and then iterate through range(int(self.line.text))
.