簡體   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