-1
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)

Hello, I need some help in storing the output after the "download" button is clicked,what I want is when "download" button is clicked the value to be stored to_download (if an input is placed not None) and the window to close.

Right now the window is closing but it doesn't store the value

  • First you have to understand [Event-driven programming](https://stackoverflow.com/a/9343402/7414759) and [Tkinter understanding mainloop](https://stackoverflow.com/questions/29158220/tkinter-understanding-mainloop). Read up on [Button.config-method - option `command=`](http://effbot.org/tkinterbook/button.htm#Tkinter.Button.config-method) – stovfl Feb 17 '20 at 14:56

2 Answers2

2

Hi you should set a function to command and do this in function

import tkinter

def 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()
CC7052
  • 563
  • 5
  • 16
0

This line was probably accidentally omitted from the original code. It should be inserted in the press function before the if statement:

to_download = entry.get()

Here is the complete code:

import tkinter

def 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()
Scott
  • 367
  • 1
  • 9