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)
CodePudding user response:
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()
CodePudding user response:
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 )