Unable to complete operation on element with key none

Question:

I’m practicing with an algorithm that generates a random number that the user needs, then keeps trying until it hits. But PySimpleGUI produces an error saying: Unable to complete operation on element with key None.

import randomimport PySimpleGUI as sg

class ChuteONumero:
    def init(self):
        self.valor_aleatorio = 0
        self.valor_minimo = 1
        self.valor_maximo = 100
        self.tentar_novamente = True
    def Iniciar(self):
        # Layout
        layout = [
            [sg.Text('Seu chute', size=(39, 0))],
            [sg.Input(size=(18, 0), key='ValorChute')],
            [sg.Button('Chutar!')],
            [sg.Output(size=(39, 10))]
        ]
        # Criar uma janela
        self.janela = sg.Window('Chute o numero!', Layout=layout)
        self.GerarNumeroAleatorio()
        try:
            while True:
                # Receber valores
                self.evento, self.valores = self.janela.Read()
                # Fazer alguma coisa com os vaalores
                if self.evento == 'Chutar!':
                    self.valor_do_chute = self.valores['ValorChute']
                    while self.tentar_novamente == True:
                        if int(self.valor_do_chute) > self.valor_aleatorio:
                            print('Chute um valor mais baixo')
                            break
                        elif int(self.valor_do_chute) < self.valor_aleatorio:
                            print('Chute um valor mais alto!')
                            break
                        if int(self.valor_do_chute) == self.valor_aleatorio:
                            self.tentar_novamente = False
                            print('Parabéns, você acertou!')
                            break
        except:
            print('Não foi compreendido, apenas digite numeros de 1 a 100')
            self.Iniciar()

def GerarNumeroAleatorio(self):
    self.valor_aleatorio = random.randint(
        self.valor_minimo, self.valor_maximo)

chute = ChuteONumero()
chute.Iniciar()

I expected a layout to open, but it does not open.

Answers:

Revised your code …

import random
import PySimpleGUI as sg

class ChuteONumero:

    def __init__(self):
        self.valor_aleatorio = 0
        self.valor_minimo = 1
        self.valor_maximo = 100
        self.tentar_novamente = True

    def Iniciar(self):
        # Layout
        layout = [
            [sg.Text('Your kick', size=(39, 0))],
            [sg.Input(size=(18, 0), key='ValorChute')],
            [sg.Button('Kick!')],
            [sg.Output(size=(39, 10))]
        ]
        # Create a window
        self.janela = sg.Window('Guess The Number!', layout)
        self.GerarNumeroAleatorio()
        while True:
            # Receive amounts
            evento, valores = self.janela.read()
            if evento == sg.WIN_CLOSED:
                break
            # Do something with the values
            elif evento == 'Kick!':
                try:
                    valor_do_chute = int(valores['ValorChute'])
                except ValueError:
                    print('Not understood, just type numbers from 1 to 100')
                    continue
                if valor_do_chute > self.valor_aleatorio:
                    print('Guess a lower value')
                elif valor_do_chute < self.valor_aleatorio:
                    print('Kick a higher value!')
                if valor_do_chute == self.valor_aleatorio:
                    sg.popup_ok('Congratulations, you got it right!')
                    break
        self.janela.close()

    def GerarNumeroAleatorio(self):
        self.valor_aleatorio = random.randint(self.valor_minimo, self.valor_maximo)

chute = ChuteONumero()
chute.Iniciar()
Answered By: Jason Yang
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.