如何将 Entry Widget Tkinter 保存为变量

How to save a Entry Widget Tkinter as a variable

我正在尝试制作一个接受 url 和 return return 的应用程序,url 返回以使用 tkinter 进行处理。但我已经尝试了一切,但没有用。我该怎么做我正在尝试保存 txtfld 条目变量。我尝试传递该变量,但它不起作用

from tkinter import *
from TikTokApi import TikTokApi

# This is generating the tt_webid_v2 cookie
api = TikTokApi.get_instance()

window=Tk()
window.iconbitmap("unnamed.ico")
img = PhotoImage(file="dw.png")
label = Label(
    window,
    image=img
)
label.place(x=0, y=0)


lbl=Label(window, text="Sup", fg='red', font=("Helvetica", 16))
lbl.place(x=40, y=50)
txtfld=Entry(window, text="This is Entry Widget", bd=1)
txtfld.place(x=100, y=120, width=300,height=20)
txtfld.get()

# This is generating the tt_webid_v2 cookie
# need to pass it to methods you want to download
device_id = api.generate_device_id()
tiktoks = api.get_tiktok_by_url(txtfld)
# Defining mp4 bytes
video_bytes = api.get_video_by_tiktok(tiktoks, custom_device_id=device_id)
def download():
    with open("d.mp4", "wb") as out:
        out.write(video_bytes)
btn=Button(window, text="Download", fg='blue', command=download)
btn.place(x=210, y=170)
btn.pack()
window.title('sup')
window.geometry("500x300+30+30")
# need to pass it to methods you want to download

window.mainloop()

谢谢

如果我没理解错的话,您需要将文本输入到输入框中。您只需要 get 方法即可。

url = txtfld.get()

因此,如果您要在输入文本和用户单击 return 时将一些操作绑定到输入框,您将得到如下内容:

def action():
    url = txtfld.get()
    # do something with url

txtfld.bind('<Return>', lambda _: action())

或一个按钮:

button = Button(window, text="Submit", command=action)

将 url 存储在变量中后,您可以使用 How to read html from a url... 上的答案来获取 html。