First of all, my UI is this:
Well! I make a "Code generator". I have a blank QListWidget, which I want to have codes. These codes will making with 'Create' button. Until now, I was testing the script, seeing the codes to be printed on the terminal. Now, I want to see them in the QListWidget. So, when I click the button, a new code will appear to the QListWidget. First, codes go to Codes_list = []
with this way:
class Ui_MainWindow_2_1(object):
def __init__(self):
self.Codes_list = []
def Code_maker(self):
import random
chars= "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!$%&*#/\?[]{}-_"
for x in range(self.HowMany_SpinBox.value()):
password = ""
for y in range(0, 10):
password_char = random.choice(chars)
password += password_char
print(password)
self.Codes_list.append(password)
def setupUi(self, MainWindow_2_1):
# Some code later...
self.CreateButton.clicked.connect(self.Code_maker)
For example, the Codes_list
could have these codes: ["rdt5u\&/EQ", "BJnV?zNOXn", "TnRZEMjbd6", "}?TF/hmTDF", "y9D1-Cl-Hk"]
. Now, from here, codes should transfer to the QListWidget... How?
My QListWidget script is this:
class Ui_MainWindow_2_1(object):
# Some code later...
def setupUi(self, MainWindow_2_1):
# Some code later...
self.Codes_listWidget = QtWidgets.QListWidget(self.background)
self.Codes_listWidget.setGeometry(QtCore.QRect(20, 120, 231, 161))
self.Codes_listWidget.setStyleSheet("background-color: none;\nborder-radius: 10px;")
self.Codes_listWidget.setObjectName("Codes_listWidget")
item = QtWidgets.QListWidgetItem()
font = QtGui.QFont()
font.setFamily("Microsoft YaHei UI")
font.setWeight(50)
item.setFont(font)
self.Codes_listWidget.addItems(self.Codes_list)
I want QListWidget to update exactly when you click the 'Create' button.
The passwords on Code_maker will stay there forever.
My question solved in the comments of my question! I added self.Codes_listWidget.addItem(password)
after self.Codes_list.append(password)
. Thanks @musicamante!!!
The finally script is:
class Ui_MainWindow_2_1(object):
def __init__(self):
self.Codes_list = []
def Code_maker(self):
import random
chars= "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!$%&*#/\?[]{}-_"
for x in range(self.HowMany_SpinBox.value()):
password = ""
for y in range(0, 10):
password_char = random.choice(chars)
password += password_char
print(password)
self.Codes_list.append(password)
self.Codes_listWidget.addItem(password)
def setupUi(self, MainWindow_2_1):
# Some code later...
self.CreateButton.clicked.connect(self.Code_maker)
My finally QListWidget script is:
class Ui_MainWindow_2_1(object):
# Some code later...
def setupUi(self, MainWindow_2_1):
# Some code later...
self.Codes_listWidget = QtWidgets.QListWidget(self.background)
self.Codes_listWidget.setGeometry(QtCore.QRect(20, 120, 231, 161))
self.Codes_listWidget.setStyleSheet("background-color: none;\nborder-radius: 10px;")
self.Codes_listWidget.setObjectName("Codes_listWidget")
item = QtWidgets.QListWidgetItem()
font = QtGui.QFont()
font.setFamily("Microsoft YaHei UI")
font.setWeight(50)
item.setFont(font)