Как присвоить текст переменной после нажатия на кнопку (Python)
Хочу чтобы ПОСЛЕ нажатия на кнопку можно было ввести какой то текст
Который будет равен с переменной YouTubeUrl
def btnYou_press():
print("Скачать видео с YouTube")
YouTubeUrl = input("YouTube")
ydl_opts = {}
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
ydl.download([YouTubeUrl])
root = Tk()
root.title("Assistant")
root.geometry("300x250")
#Кнопка
btn = Button(text="Скачать видео с YouTube",
background="#555",
foreground="#ccc",
padx="20",
pady="8",
font="16",
command = btnYou_press
)
btn.pack()
#Кнопка
root.mainloop()
Ответы (1 шт):
Автор решения: day383
→ Ссылка
Можно добавить вторую кнопку, которая вместе с текстовым полем будет отображаться по нажатию первой кнопки. Первая же кнопка после нажатия будет скрываться.
import youtube_dl
from tkinter import Tk, Button, Text
def btn_hello_handler():
textBox.pack(padx=5, pady=40)
btn_hello.pack_forget()
btn_download.pack()
def btn_download_handler():
YouTubeUrl = retrieve_input().strip()
ydl_opts = {}
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
ydl.download([YouTubeUrl])
def retrieve_input():
return textBox.get('1.0', 'end-1c')
root = Tk()
root.title("YT downloader")
root.geometry("400x250")
# Кнопка приветствия
btn_hello = Button(text="Скачать видео с YouTube",
background="#555",
foreground="#ccc",
padx="20",
pady="8",
font="16",
command = btn_hello_handler
)
# Поле ввода
textBox = Text(root, height=2, width=40)
# Кнопка скачивания
btn_download = Button(text="Скачать",
background="#555",
foreground="#ccc",
padx="20",
pady="8",
font="16",
command = btn_download_handler
)
btn_hello.pack()
root.mainloop()