2015-02-04 4 views
1

입력 widget의 내용을 버튼에 전달하는 방법을 찾고 있습니다. 복잡한 부분은 주어진 XML 구조에서 동적으로 생성 된 버튼과 엔트리 위젯이 버튼과 매개 변수가있는 GUI를 설명하는 'xmlString'을 볼 때 버튼과 매개 변수의 개수가 가변적이라는 것입니다.Tkinter의 Entry 위젯에서 동적으로 생성 된 Button에 매개 변수 전달

나는 XML을 단순화하고 데모 코드를 만들었습니다. 람다 함수를 알고 있지만 동적 작성된 입력 위젯을 사용하여이를 수행하는 방법을 모릅니다.

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

답변

0

작성한 모든 항목의 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'] 
+0

감사합니다, 이것은 매우 좋은 솔루션입니다, 하나는 너무 매우 빠른 이해 :) –

관련 문제