2017-01-07 5 views
0

Ich entwickle ein einfaches Programm und ich muss den Wert von einer Combobox erhalten. Es ist einfach, wenn die Combobox in den ersten erstellten Fenstern ist, aber zum Beispiel, wenn ich zwei Fenster habe und die Combobox in der Sekunde ist, kann ich den Wert nicht lesen.Get Combobox Wert in Python

Zum Beispiel:

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

Wenn ich die Eltern des Widgets von ciao Mutter ändern funktioniert es! Kann mir jemand erklären? Vielen Dank im Voraus und Entschuldigung für mein schlechtes Englisch

+2

Tkinter nicht gut handhaben zwei Hauptfenster, so sollte das zweite ein Toplevel des ersten sein. –

Antwort

1

Sie können nicht zwei Tk() Fenster haben. man muss Toplevel sein.

die Variable erhalten Sie box_value.get()

Beispiel einer Drop-Down-Box

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 

print (self.current_table.get())

+0

Danke! Es klappt! Ich verstehe jetzt – Damien