2015-02-05 119 views
0
variables = [] 
entries = [] 
va = StringVar() #variable 
en = Entry(frame, textvariable=va) #entrybox 
en.grid(row=i+1, column=4) 
variables.insert(i,va) 
entries.insert(i,en) 

我想獲得用戶輸入到列表中,我複製了這段代碼,但我很困惑用戶輸入存儲在哪個列表? 我的意圖是生成一列輸入框,並將用戶輸入寫入列表。寫tkinter條目輸入列表

+0

因此,每個條目的內容將是一個列表中的值?像'contents = [entry1_value,entry2_value,entry3_value]'? – nbro 2015-02-05 14:54:18

回答

0

entries = []存儲的是用戶輸入,因爲entries.insert(i,en)其中enEntryi可能等於0

+1

「條目」與「用戶輸入」不同。前者是出現在GUI上的文本框,後者是該文本框內的字符串。 – Kevin 2015-02-05 14:53:16

+0

@Kevin是不是'textvariable = va'是文本框中的默認參數?我認爲在輸入框中有一個默認文本,然後每當用戶輸入時,將其插入到'entries'中 – GLHF 2015-02-05 14:53:55

0

首先,你不需要StringVar對象設置得到Entry對象的內容。 StringVar s主要用於與不同的小部件共享相同的值,我認爲情況並非如此。

您可以做的是獲取在Entry小部件中插入值。您可以通過指定指數(和)的條目插入一個字符串:

import tkinter as tk 

m = tk.Tk() 
e = tk.Entry(m) 
e.pack() 
e.insert(0, "Goodbye!") # index=0, string="Goodbye" 
m.mainloop() 

爲了得到一個項的內容,這是更簡單(以同樣的例子):

import tkinter as tk 

m = tk.Tk() 
e = tk.Entry(m) 
e.pack() 
e.insert(0, "Goodbye!") 
b = tk.Button(m, text="Get Entry's content", 
       command=lambda: print(e.get())) # on click, the contents will be printed 
b.pack() 
m.mainloop() 

在你的情況,而不是具有lambda,您可以簡單地正常功能在下列方式一鍵命令關聯:

b = Button(command=get_entries_content) # note the absence of() 

然後你可以定義你的get_entries_content函數,它會得到你單個條目的內容,然後用它們來填充一個列表。

def get_entries_content(): 
    # get contents of Entries 
    # fill my list