Bug when trying to use Pysimplegui

Asked

Viewed 182 times

0

I’m learning how to use Simplepythongui, but I’m encountering a bug. The problem is this: when I get to if down there, even if the number random and the one chosen by the user are equal, the program does not give the print that the user has won and always goes to the else.

import PySimpleGUI as pg
from random import randint
class telapython:
    def __init__(self):
        #layout
        layout = [
            [pg.Text('Digite um número') , pg.Input(key= 'Pergunta')],
            [pg.Button('Verificar número')],

        ]

        #janela

        janela = pg.Window('Adivinhe o número').layout(layout)

        #extração de dados
        self.button , self.values = janela.Read()

    def iniciar(self):
        random = randint(0, 1)
        pergunta = self.values['Pergunta']
        print(random , pergunta)
        if random == pergunta:
            print('Você venceu')
        else:
            print('Você errou')

tela = telapython()
tela.iniciar()

3 answers

0

I’m advanced in the Tkinter library, a library similar to pysimpleGUI. When I spin your code only this part works super well... Congratulations!

    def iniciar(self):
    random = randint(0, 1)
    pergunta = self.values['Pergunta']
    print(random , pergunta)
    if random == pergunta:
        print('Você venceu')
    else:
        print('Você errou')

It seems that pergunta = self.values['Pergunta'] is not taking the value you put, in Tkinter use the .get(), so that’s why it goes straight to IS, but in pysimpleGUI (a search I found) it uses the .read(). Try to use something like:

pergunta = self.values['Pergunta'].read()
pergunta = self.values.read(['Pergunta'])
pergunta.read() = self.values['Pergunta']

I hope it helped,

-1

By default when you receive the Pysimplegui Input it comes with 'str' you have to turn into 'int' as follows.

_pergunta_ = self.values['Pergunta']
_pergunta_ = int(_pergunta_)

-1

Use the conversion to int before establishing equality:

if random == int(pergunta):

Browser other questions tagged

You are not signed in. Login or sign up in order to post.