0

How can I pass the input I receive with Tkinter to the getLink function? I want it to send the input to the function when I press the button.

import tkinter as tk
import requests

pencere=tk.Tk()
pencere.title("İnstagram Share App")
pencere.geometry("360x480")

def getLink(url):
    r = requests.get(url)
    with open('00.jpeg', 'wb') as f:
        f.write(r.content)

def buton_link():
    link = ent1.get()
    return link

e1=tk.Label(text="Image Link",font="Arial 12 bold")
e1.pack()
ent1=tk.Entry(width=30)
ent1.pack()

b1=tk.Button(text="Link",bg="black",fg="white",font="Arial 20 bold",command=buton_link())
b1.pack()

pencere.mainloop()

link = buton_link()
getLink(link)

Delrius Euphoria
  • 14,910
  • 3
  • 15
  • 46
Axciom
  • 33
  • 2
  • 1
    Does this answer your question? [Why is my Button's command executed immediately when I create the Button, and not when I click it?](https://stackoverflow.com/questions/5767228/why-is-my-buttons-command-executed-immediately-when-i-create-the-button-and-no) – Delrius Euphoria Aug 17 '22 at 14:37

2 Answers2

1

when you cast buton_link() into Button, you acctualy are calling the fuction before press the button. Use only buton_link instead.

import tkinter as tk
import requests

pencere=tk.Tk()
pencere.title("İnstagram Share App")
pencere.geometry("360x480")

def getLink(url):
    r = requests.get(url)
    with open('00.jpeg', 'wb') as f:
        f.write(r.content)

def buton_link():
    link = ent1.get()
    getLink(link)

e1=tk.Label(text="Image Link",font="Arial 12 bold")
e1.pack()
ent1=tk.Entry(width=30)
ent1.pack()

b1=tk.Button(text="Link",bg="black",fg="white",font="Arial 20 bold",command=buton_link)
b1.pack()

pencere.mainloop()
0

You are calling the function and only returning the value ! Which you shouldn't in the below line b1=tk.Button(text="Link",bg="black",fg="white",font="Arial 20 bold",command=buton_link())

Instead you should just write the name of function in command argument like this : b1=tk.Button(text="Link",bg="black",fg="white",font="Arial 20 bold",command=buton_link)

( Without Brackets )

Tuchar Das
  • 43
  • 6