How to pass the value of Sg. Input to a variable in another part of the code?

Asked

Viewed 54 times

-2

I’m doing a program to present in the college project, but I’m doing it through the window method by pySimpleGUI, I wanted when the person typed in Sg. Input a quantity, that quantity was calculated times the price of the product that would be in the table, tried in the ways that I knew just didn’t work, wanted to try to do by database only that also not found a way to take the data to the database and then pull back to the program, someone could help me solve if please.

code:

import PySimpleGUI as sg
import sqlite3 

banco =sqlite3.connect("variaveis.db")

#janela inicial
def janela_inicial():
    sg.theme("Reddit")
    layout =[
        [sg.Button("Comprar")],
        [sg.Button("Informações")],
        [sg.Button("Sair")]
    ]
    return sg.Window("Inicio", layout= layout, finalize= True)

#janela de compras
def janela_compras():
    sg.theme("Reddit")
    layout = [
        [sg.Text("Produtos:"), sg.Text("            Quantidade:")],
        [sg.Text("[1] - Maçã: R$0,75"), sg.Input(key= 'quantM', size= (20,1))],
        [sg.Text("[2] - Feijão: R$5,00"), sg.Input(key= 'quantF', size= (20,1))],
        [sg.Text("[3] - Arroz: R$3,00"), sg.Input(key= 'quantA', size= (20,1))],
        [sg.Button("Voltar"), sg.Button("Finalizar compra")]
    
    ]
    return sg.Window("Compras", layout= layout, finalize= True)

#janela de conta
def janela_conta():
    sg.theme("Reddit")
    layout = [
        [sg.Text("===================")],
        [sg.Text("               Nota")],
        [sg.Text("===================")],
        [sg.Text("Produto:"), sg.Text("   Total:")],
        [sg.Text("Maçã: R$")],
        [sg.Text("--------------------------------------")],
        [sg.Text("Feijão: R$")],
        [sg.Text("--------------------------------------")],
        [sg.Text("Arroz: R$")],
        [sg.Text("--------------------------------------")],
        [sg.Text("Total: R$")],
        [sg.Button("Voltar"), sg.Button("Finalizar compra")]
    ]
    return sg.Window("Nota", layout= layout, finalize= True)

def janela_informacoes():
    sg.theme("Reddit")
    layout = [
    [sg.Text("Nenhuma informação no momento!")],
    [sg.Button('Voltar')]
    ]
    return sg.Window("Nota", layout= layout, finalize= True)

#janelas iniciais
janela1, janela2, janela3, janela4 = janela_inicial(), None, None, None

#loop leitura de eventos
while True:
    window, event, value = sg.read_all_windows()

    #quando  a janela for fechada
    if window == janela1 and event == sg.WIN_CLOSED:
        break 
    elif window == janela2 and event == sg.WIN_CLOSED:
        break
    elif window == janela3 and event == sg.WIN_CLOSED:
        break
    elif window == janela4 and event == sg.WIN_CLOSED:
        break 

    #proxima janela compras
    elif window == janela1 and event == 'Comprar':
        janela2 = janela_compras()
        janela1.hide()

    #proxima janela informações
    elif window == janela1 and event == 'Informações':
        janela4 = janela_informacoes()
        janela1.hide()

    #proxiam janela conta
    elif window == janela2 and event == 'Finalizar compra':
        janela3 = janela_conta()
        janela2.hide()

    #voltar janela comprar para inicial
    if window == janela2 and event == "Voltar":
        janela2.hide()
        janela1.un_hide()

    #voltar janela informaçoes para inicial
    if window == janela4 and event == "Voltar":
        janela4.hide()
        janela1.un_hide()

    #janela fechar programa
    if window == janela1 and event == "Sair":
        break

#voltar janela conta para comprar
if window == janela3 and event == "Voltar":
    janela3.hide()
    janela2.un_hide()

#janela pop-up
elif window == janela3 and event == 'Finalizar compra':
    sg.popup("Obrigado pela compra e volte sempre!")

'''

1 answer

0

He got a little snippy, but it worked.

First: Forget SQL. For this code ai, it is an Overkill. But if you want to expand this application, SQL may be a good idea.

Second: Make the following changes to the account window:

#janela de conta
def janela_conta():
    sg.theme("Reddit")
    layout = [
        [sg.Text("===================")],
        [sg.Text("               Nota")],
        [sg.Text("===================")],
        [sg.Text("Produto:"), sg.Text("   Total:")],
        [sg.Text(key= "TvalM", size=(20,1))],
        [sg.Text("--------------------------------------")],
        [sg.Text(key= "TvalF", size=(20,1))],
        [sg.Text("--------------------------------------")],
        [sg.Text(key= "TvalA", size=(20,1))],
        [sg.Text("--------------------------------------")],
        [sg.Text(key= "TvalTot", size=(20,1))],
        [sg.Button("Voltar"), sg.Button("Finalizar compra")]
    ]
    return sg.Window("Nota", layout= layout, finalize= True)

Third: make the following amendment while:

    #proxima janela conta
    elif window == janela2 and event == 'Finalizar compra':
        janela3 = janela_conta()
        
        valM = int(value['quantM'])*0.75
        valA = int(value['quantA'])*3.00
        valF = int(value['quantF'])*5.00
        valTot = valM + valA + valF
        
        janela3["TvalM"].update(value['quantM'] + ' * R$ 0,75 = %.2f' %(valM))
        janela3['TvalF'].update(value['quantF'] + ' * R$ 5,00 = %.2f' %(valF))
        janela3['TvalA'].update(value['quantA'] + ' * R$ 3,00 = %.2f' %(valA))
        janela3['TvalTot'].update('Total = %.2f' %(valTot))
        janela2.hide()

There will already work the question of you use the values of the variables of sg.input. It’s up to you now to organize the code the way you prefer.

  • Thanks for the help, man !!

Browser other questions tagged

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