存储来自文本框 tkinter 的输入

import tkinter


window = tkinter.Tk()

window.configure(background="grey90")

window.title("Downloader")

window.geometry("300x300")

window.resizable(False, False)


entry = tkinter.Entry(window)

entry.place(x=70,y=68)

entry.configure(highlightbackground="grey90")


button = tkinter.Button(window, text="Download",

command=window.destroy, highlightbackground="grey90")

button.place(x=110,y=120)

to_download = entry.get()

window.mainloop()

print(to_download)

您好,在单击“下载”按钮后,我需要一些帮助来存储输出,我想要的是单击“下载”按钮时要存储的值(如果未放置输入)和要关闭的窗口。to_downloadNone


现在窗口正在关闭,但它没有存储值


隔江千里
浏览 86回答 2
2回答

梵蒂冈之花

你应该设置一个函数,并在函数中执行此操作commandimport tkinterdef press():    global to_download    if to_download:        print(to_download)        window.destroy()window = tkinter.Tk()window.configure(background="grey90")window.title("Downloader")window.geometry("300x300")window.resizable(False, False)entry = tkinter.Entry(window)entry.place(x=70,y=68)entry.configure(highlightbackground="grey90")button = tkinter.Button(window, text="Download",command=press, highlightbackground="grey90")button.place(x=110,y=120)window.mainloop()

慕妹3146593

此行可能不小心从原始代码中省略。它应该插入到 press 函数中 if 语句之前:to_download = entry.get()以下是完整的代码:import tkinterdef press():    global to_download    to_download = entry.get()    if to_download:        print(to_download)        window.destroy()window = tkinter.Tk()window.configure(background="grey90")window.title("Downloader")window.geometry("300x300")window.resizable(False, False)entry = tkinter.Entry(window)entry.place(x=70,y=68)entry.configure(highlightbackground="grey90")button = tkinter.Button(window,                        text="Download",                        command=press,                        highlightbackground="grey90")button.place(x=110,y=120)window.mainloop()
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python