2012-10-23 25 views
0
url=StringVar() 
txt1=Entry(root,textvariable=url) 
txt1.pack() 

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

確定這是我的基本GUI ......我有我想要的功能,使用下面使用Python(解析)一個文本框的值作爲字符串轉換爲功能

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

一個文本框txt1中我目標是提供文本框中的URL,然後在我的函數downloadfile()中拆分URL,但是我的url變量變成PY_VAR0而不是「www.example.com/file.exe」

和錯誤消息I得到的是「StringVar實例沒有屬性拆分」 我在做一些非常錯誤的事情,但我不知道即 任何人都可以幫忙嗎?

回答

2

StringVar只是一個「字符串變量的值持有者」。爲了得到它的內容(字符串)使用方法:

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

直接打印STRINGVAR(「打印網址」)調用:

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

這將返回內部變量的名字,而不是它的價值。在您的代碼使用:

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

謝謝!你是老闆:) – scandalous

+0

請幫助這個https://pastebin.com/uPWyPXMZ和這個https://stackoverflow.com/questions/45767638/how-to-pass-a-textbox-entry-to-variables-使用-Tkinter的功能於蟒蛇 –

相關問題