简体   繁体   English

重用按钮

[英]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.第二个 function 中的print(x)用于检查变量是否已从第一个传递。 After passing the x my program doesn't do anything else.通过 x 后,我的程序不执行任何其他操作。

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.您的按钮所连接的 function 之后必须使用括号调用。 eg.例如。 cantidad_items() in order to call a function, you need to use parenthases after the function name. cantidad_items() 为了调用 function,您需要在 function 名称后使用括号。 Also, your function doesn't have much happening inside it, which contains all the things that will happen once you press the button.此外,您的 function 内部没有发生太多事情,其中包含按下按钮后将发生的所有事情。

This website might help.这个网站可能会有所帮助。 Sorry it isn't in spanish!对不起,不是西班牙语! https://www.w3schools.com/python/python_functions.asp https://www.w3schools.com/python/python_functions.asp

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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