M
M
mcmikey2021-07-14 19:16:11
Python
mcmikey, 2021-07-14 19:16:11

How to make a button in PySimpleGUI work?

I made a simple window with a number generator in pysimplegui and everything works except for the Generate button. I do not understand why the text does not take the desired value.

import PySimpleGUI as sg
import random

sg.theme("Dark Green")

num = ""

def generate():
  x = values["FROM"]
  y = values["TO"]
  num = random.randint(int(x), int(y))
  return num

layout = [
  [sg.Text("From:"), sg.Input(size = (5, 1), enable_events = True, key = ("FROM"))],
  [sg.Text("To:"), sg.Input(size = (5, 1), enable_events = True, key = ("TO"))],
  [sg.Button("Generate:", key = ("GENERATE")), sg.Text(num)],
]

window = sg.Window("Random numbers", layout, size = (150, 100))

while True:
  event, values = window.read()
  if event == sg.WIN_CLOSED:
    break
  elif event == "GENERATE":
    generate()
window.close()

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Drill, 2021-07-15
@mcmikey

[sg.Button("Generate:", key = ("GENERATE")), sg.Text(num, key = 'OUTPUT')],

elif event == "GENERATE":
    values['OUTPUT'] = generate()
    window['OUTPUT'].update(values['OUTPUT'])

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question