Reputation: 13
I'm making a GUI program that there are two buttons. When I click the first button, I want to generating a random code. When I click the second button, I want to see a list of the codes that generated from the first button. (For now, just to print the codes-list, into the 'Run' window.) My script is the following:
class Ui_Main_Window(object):
# For the first button:
def Code_maker(self):
import random
Codes_list = []
chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!$%^&*`#/\?[]{}-_~"
for x in range(1):
password = ""
for y in range(0, 10):
password_char = random.choice(chars)
password += password_char
print(password)
Codes_list.append(password)
# For the second button:
def show_list(self):
print (Codes_list)
# To connect the actions with the buttons:
def setupUi(self, Main_Window):
# After some code...
self.Button_New_code.clicked.connect(self.Code_maker)
# After some code...
self.Button_Show_list.clicked.connect(self.show_list)
# The continuation of the program...
My problem is that when I click the first button, the code genarating successfully and it printing into the 'Run' window. But when I click the second button the program crashes and after some seconds, it closes. I get this error:
> Unresolved reference 'Codes_list'.
(I use Pycharm and the error doesn't showing into the 'Run' window, but into the 'Problems' window.)
I tried to repair it with a global list, but the program works worse. It crashes when I click anything of the two buttons... If I write code with this way, it becomes:
class Ui_Main_Window(object):
Codes_list = []
# For the first button:
def Code_maker(self):
import random
global Codes_list
chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!$%^&*`#/\?[]{}-_~"
for x in range(1):
password = ""
for y in range(0, 10):
password_char = random.choice(chars)
password += password_char
print(password)
Codes_list.append(password)
# For the second button:
def show_list(self):
print (Codes_list)
# To connect the actions with the buttons:
def setupUi(self, Main_Window):
# After some code...
self.Button_New_code.clicked.connect(self.Code_maker)
# After some code...
self.Button_Show_list.clicked.connect(self.show_list)
I searched about my problem in Google, but I tried so many things, that I don't remember what I tried. Nothing worked.
Upvotes: 1
Views: 101
Reputation: 344
This should work. I have added comments on the lines I have added or modified for your reference. Can you please check?
class Ui_Main_Window(object):
def __init__(self):
# added the constructor to attach the list with the object itself
self.Codes_list = [] #this will contain the numbers
# For the first button:
def Code_maker(self):
import random
#Codes_list = [] # Removed
chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!$%^&*`#/\?[]{}-_~"
for x in range(1):
password = ""
for y in range(0, 10):
password_char = random.choice(chars)
password += password_char
print(password)
self.Codes_list.append(password) # modified to update the object attribute
# For the second button:
def show_list(self):
print (self.Codes_list) # modified to access the object attribute
# To connect the actions with the buttons:
def setupUi(self, Main_Window):
# After some code...
self.Button_New_code.clicked.connect(self.Code_maker)
# After some code...
self.Button_Show_list.clicked.connect(self.show_list)
# The continuation of the program...
Upvotes: 1