【问题标题】:How to get input for functions with tkinter?如何使用 tkinter 获取函数的输入?
【发布时间】:2022-08-17 22:53:53
【问题描述】:

如何将通过 Tkinter 收到的输入传递给 getLink 函数?我希望它在我按下按钮时将输入发送到函数。

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)

标签: python tkinter


【解决方案1】:

您正在调用函数并且只返回值!你不应该在下面一行 b1=tk.Button(text="Link",bg="black",fg="white",font="Arial 20 bold",command=buton_link())

相反,您应该只在命令参数中写下函数的名称,如下所示: b1=tk.Button(text="Link",bg="black",fg="white",font="Arial 20 bold",command=buton_link)

(不带括号)

【讨论】:

    【解决方案2】:

    当您将 button_link() 转换为 Button 时,您实际上是在按下按钮之前调用了该功能。仅使用 button_link 代替。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-27
      相关资源
      最近更新 更多