2016-08-16 93 views
0

我正在試圖Tkinter與Combox按 按鈕來輸出一個變量,但不要在這裏超前。蟒蛇組合框與返回變量

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