繁体   English   中英

重用按钮

[英]Reusing a button

我做了一个随机发生器来选择我想看的电影。 首先,程序会询问您电影的数量,并且需要这些电影的名称。 根据您之前输入的数字,您将尝试介绍电影。 为此,必须“回收”将条目信息推送到程序中的按钮。 第二个 function 中的print(x)用于检查变量是否已从第一个传递。 通过 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()

您的按钮所连接的 function 之后必须使用括号调用。 例如。 cantidad_items() 为了调用 function,您需要在 function 名称后使用括号。 此外,您的 function 内部没有发生太多事情,其中包含按下按钮后将发生的所有事情。

这个网站可能会有所帮助。 对不起,不是西班牙语! https://www.w3schools.com/python/python_functions.asp

暂无
暂无

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

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