2017-01-07 4 views
0

쉬운 프로그램을 개발 중이며 콤보 상자에서 값을 얻어야합니다. 콤보 박스가 처음 생성 된 윈도우에있을 때 쉽습니다. 예를 들어 두 개의 윈도우가 있고 두 번째 콤보 박스가 있으면 값을 읽을 수 없습니다. 예를 들어파이썬에서 콤보 상자 값을 얻으십시오.

: 나는 부모에게 챠오에서 위젯의 부모를 변경하는 경우

from tkinter import * 
from tkinter import ttk 

def comando(): 
    print(box_value.get()) 

parent = Tk() #first created window 
ciao=Tk()  #second created window 
box_value=StringVar() 
coltbox = ttk.Combobox(ciao, textvariable=box_value, state='readonly') 
coltbox["values"] = ["prova","ciao","come","stai"] 
coltbox.current(0) 
coltbox.grid(row=0) 
Button(ciao,text="Salva", command=comando, width=20).grid(row=1) 
mainloop() 

가 작동! 누구든지 설명 할 수 있습니까? 미리 감사드립니다. 나쁜 영어로 죄송합니다.

+2

Tkinter를 잘 처리하지 않습니다 (self.current_table.get()) 인쇄 두 개의 메인 윈도우, 그래서 두 번째는 첫 번째의 최상위이어야합니다. –

답변

1

두 개의 Tk() 창을 사용할 수 없습니다. 하나는 탑 레벨이어야합니다.

당신이 box_value.get 않는 변수() 드롭 다운 상자의

예를 얻을 수

class TableDropDown(ttk.Combobox): 
    def __init__(self, parent): 
     self.current_table = tk.StringVar() # create variable for table 
     ttk.Combobox.__init__(self, parent)# init widget 
     self.config(textvariable = self.current_table, state = "readonly", values = ["Customers", "Pets", "Invoices", "Prices"]) 
     self.current(0) # index of values for current table 
     self.place(x = 50, y = 50, anchor = "w") # place drop down box 

+0

감사합니다. 그것은 작동합니다! 지금은 이해 – Damien

관련 문제