[英]Passing parameters to a dynamically created Button from an Entry widget in Tkinter
我正在尋找一種方法,如何將條目小部件的內容傳遞給按鈕。 對我而言,最復雜的部分是從給定的XML結構動態創建的按鈕和輸入小部件(請參見“ xmlString”),其中描述了具有按鈕和參數的GUI,按鈕和參數的數量是可變的。
我簡化了XML並制作了演示代碼。 我知道lambda函數,但是我不知道如何使用動態創建的條目窗口小部件來執行此操作。
from Tkinter import *
import xml.etree.ElementTree as ET
rootframe = Tk()
def runScript(text = 'default text'):
print 'runScript %s'%text
xmlString = '''
<window>
<Tab id="tabpage5" name="Debugging" type="custom">
<command dest="1" mode="CONFIG" unit="4" id="SET_TIMEOUT" type="COMMAND">
<initiator name="Set Timeout" type="button" />
<parameter editable="true" param_name="PARAM1">31536000</parameter>
</command>
<command dest="1" mode="CONFIG" unit="4" id="SET_thing" type="COMMAND">
<initiator name="Set Timeout" type="button" />
<parameter param_name="PARAM1" >31536000</parameter>
<parameter param_name="PARAM2">5</parameter>
</command>
<command />
</Tab>
</window>
'''
xmlRoot = ET.fromstring(xmlString)
for tab in xmlRoot.iter('Tab'):
row = 0
column = 0
for command in tab.iter('command'):
for tag in command.iter() :
#not sure why command tag is here but skipping it
if tag.tag == 'command':
pass
continue
if tag.tag == 'initiator' and tag.attrib['type'] == 'button':
button = Button(rootframe, text=tag.attrib['name'], command=lambda : runScript('nondefault text'))
button.grid(row=row, column=column, sticky='w')
column +=1
elif tag.tag == 'parameter':
entry = Entry(rootframe)
entry.insert(0,tag.text)
entry.grid(row=row, column=column)
column +=1
row +=1
column = 0
rootframe.mainloop()
您可以保留所有創建條目的2D列表。 然后,當您要訪問它們時,只需插入行號。 實施示例:
from Tkinter import *
import xml.etree.ElementTree as ET
rootframe = Tk()
def runScript(text = 'default text', row=None):
entries = entries_by_row[row] if row is not None else []
print 'runScript {}. Contents of entries: {}'.format(text, [entry.get() for entry in entries])
xmlString = '''
<window>
<Tab id="tabpage5" name="Debugging" type="custom">
<command dest="1" mode="CONFIG" unit="4" id="SET_TIMEOUT" type="COMMAND">
<initiator name="Set Timeout" type="button" />
<parameter editable="true" param_name="PARAM1">31536000</parameter>
</command>
<command dest="1" mode="CONFIG" unit="4" id="SET_thing" type="COMMAND">
<initiator name="Set Timeout" type="button" />
<parameter param_name="PARAM1" >31536000</parameter>
<parameter param_name="PARAM2">5</parameter>
</command>
<command />
</Tab>
</window>
'''
xmlRoot = ET.fromstring(xmlString)
entries_by_row = []
for tab in xmlRoot.iter('Tab'):
row = 0
column = 0
for command in tab.iter('command'):
entries_by_row.append([])
for tag in command.iter() :
#not sure why command tag is here but skipping it
if tag.tag == 'command':
pass
continue
if tag.tag == 'initiator' and tag.attrib['type'] == 'button':
button = Button(rootframe, text=tag.attrib['name'], command=lambda row=row: runScript('nondefault text', row))
button.grid(row=row, column=column, sticky='w')
column +=1
elif tag.tag == 'parameter':
entry = Entry(rootframe)
entry.insert(0,tag.text)
entry.grid(row=row, column=column)
column +=1
entries_by_row[-1].append(entry)
row +=1
column = 0
rootframe.mainloop()
單擊每個按鈕后的結果:
runScript nondefault text. Contents of entries: ['31536000']
runScript nondefault text. Contents of entries: ['31536000', '5']
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.