2016-08-16 5 views
0

Я пытаюсь сделать Tkinter с Combox нажатием кнопки , чтобы вывести переменную, но не продвигайтесь сюда.python combobox с возвращаемой переменной

from tkinter import * 
import tkinter as tk 
from tkinter import ttk 

def select(): 
     name_option = var.get() 
     print (Number of choices) 


root = tk.Tk() 


var = StringVar(root) 

choices = { 
    'A': '11', 
    'B': '22', 
    'C': '33', 
    'D': '44', 
    'E': '55', 
} 


option = tk.OptionMenu(root, var, *choices) 
option.place(x = 1, y = 50, width=80, height=25) 

change_button = tk.Button(root, text="Klick me", command=select) 

change_button.place(x = 100, y = 50, width=80, height=25) 


root.mainloop() 

ответ

0

изменение

print (Number of choices)

в

print(name_option)

или просто изменить select() на:

def select(): 
    print(var.get()) 
0

Изменения тела функции:

def select(): 
     name_option = var.get() 
     num = choices[name_option] 
     print ("Number of choices", num) 

Я просто добавил num = choices[name_option], которая возвращает значение элемента (ключ) вы выбираете. А затем распечатайте num.

Смежные вопросы