简体   繁体   中英

Reusing a button

I made a randomizer to choose what movie i want to see. First, the program asksyou for the number of movies, and the requires the names of those movies. Depending on the number that you entered before are going to be the attempts that you have to introduce the movies. For that the button that pushes the information of the entry into the program has to be "recycled". The print(x) in the second function is to check if the variable has been passed from the first one. After passing the x my program doesn't do anything else.

import tkinter as tk
from tkinter import *
from tkinter import ttk
import random

#generación de ventana
window = tk.Tk()
window.title("Decidir que ver con Mora")
window.geometry("600x500")
window.configure(background="light blue")

x = None
item = ""
list = []
len_list = len(list)

def cantidad_items():
    x= int(texto1.get())
    boton1.configure(text = "Ingresar", command= lambda:añadir(x))
    
def añadir(x):
    print(x)
    if len_list < x:
        item = texto1.get()
        list.append(item)
    else:
        print(random.choice(list))

#primer entry del número
texto1 = ttk.Entry(window, font = "Helvetica 20", width = 22)
texto1.place(relx = 0.5, rely=0.45, anchor=CENTER)
#texto1.insert(0, "Ingrese la cantidad de ítems")

#botón ejecución del input
boton1 = tk.Button(window, text = "Ingresar", command = cantidad_items)
boton1.place(relx = 0.5, rely = 0.55, anchor=CENTER)

window.mainloop()

The function that your button is attached to has to be called by using parenthases afterward. eg. cantidad_items() in order to call a function, you need to use parenthases after the function name. Also, your function doesn't have much happening inside it, which contains all the things that will happen once you press the button.

This website might help. Sorry it isn't in spanish! https://www.w3schools.com/python/python_functions.asp

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM