-1
I’m learning python and I’m trying to program a platform to play table rpg
my intention was to assemble a code that would take the name of a character that would be placed in an input and add it into a variable, so that this variable would be used as the player’s 'name'. The problem is that my code does not 'save' this variable.
i am using a tool called Pysimplegui to assemble a graphical interface
import PySimpleGUI as sg
from random import randint
class ScreenRecord():
def __init__(self):
layoutRecord = [
[sg.Text('Enter character name'),sg.Input(size=(15,0),key='character')],
[sg.Button('Enter',key = 'Enter'),]
]
self.window = sg.Window('teste-mecanica').layout(layoutRecord)
self.button,self.values = self.window.Read()
def Enter(event):
character = self.values['character']
tela = ScreenDice()
while True :
event,values = self.window.Read()
if event is None:
break
if event in ['Enter']:
Enter(event)
class ScreenDice(ScreenRecord):
def __init__(self):
db = {'size':(12,2)}
layoutDice = [
[sg.Button('Roll D10',key= 'd10',**db),sg.Button('Roll D20',key='d20',**db)],
]
self.window = sg.Window('teste-dado').layout(layoutDice)
self.button,self.values = self.window.Read()
def RollDice(event):
print(character ,'received', randint(1,dice),'in the dice')
while True :
event , values = self.window.Read()
if event is None:
break
if event in ['d10'] :
dice = 10
RollDice(event)
if event in ['d20'] :
dice = 20
RollDice(event)
the terminal returns me :
NameError: name 'character' is not defined
Obs: it’s my first time programming something, forgive me if it’s a stupid question
but the line
character = self.values['character']
should not solve the problem ?– Luis Fernando Rodrigues
in Traceback went bad, put a break point at the beginning of the code and go through line by line, and see where the error occurs if pq until the result is that the variable is not declared and as it was consulted any word.
– stack.cardoso