簡體   English   中英

Python使用(解析)文本框值作為字符串到函數中

[英]Python using(parsing) a textbox value as string into a function

url=StringVar()
txt1=Entry(root,textvariable=url)
txt1.pack()

button1 = Button(root, text="Download" ,command=downloadFile)
button1.pack()
root.mainloop()

好的,這是我的基本GUI ...我有一個文本框txt1,我想在下面的函數中使用

def downloadFile():
    #print "url is:",url
    file_name = url.split('/')[-1]

我的目標是輸入文本框中的URL,然后在我的函數downloadfile()中拆分URL,但我的url變量變為PY_VAR0而不是“www.example.com/file.exe”

我得到的錯誤信息是“StringVar實例沒有屬性拆分”我做錯了但我不知道在哪里。 有人可以幫忙嗎?

StringVar只是“字符串變量的值持有者”。 要獲取其內容(字符串),請使用:

StringVar.get() # Return value of variable as string.

直接打印StringVar(“print url”)調用:

StringVar.__str__() # Return the name of the variable in Tcl.

這將返回內部變量名稱,而不是其值。 在你的代碼中使用:

file_name = url.get().split('/')[-1]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM