2017-08-30 85 views
0

我正在使用Python 3.6.0,並且僅當我點擊「確認結果」按鈕時纔想保存我的組合框的值。我已經做了一些尋找 - 但也許我有錯誤的術語 - 我找不到這樣做的方式。當按下按鈕時綁定ttkCombobox - Python

我假設問題是與我的線self.firstfaction_ent.bind(「Button-1」,self.firstfaction_onEnter)。我相當確信「Button-1」不正確 - 但我一直在嘗試一切。

from tkinter import * 
from tkinter import ttk 

class Application(Frame): 

    def __init__(self, master): 

# Initialise the Frame 
     super(Application, self).__init__(master) 
     self.grid() 
     self.create_widgets() 

    def create_widgets(self): 

    #Define Combobox for Faction 
     self.firstfaction_ent=ttk.Combobox(self,textvariable=varSymbol, state='readonly') 
     self.firstfaction_ent.bind("<Button-1>", self.firstfaction_onEnter)  
     self.firstfaction_ent['values']=Faction 
     self.firstfaction_ent.grid(row=2, column=1) 

    #create a submit button 
     self.enter_bttn = Button(self,text = "Confirm Your Results", command = self.firstfaction_onEnter).grid(row=7, column=1) 

    def firstfaction_onEnter(self, event): 
     Faction_First = varSymbol.get() 
     print(Faction_First) 

#main 

root = Tk() 

#Window Title & size 
root.title("Sports Carnival Entry Window") 
root.geometry("600x500") 

varSymbol=StringVar(root, value='') 
varSecondFaction=StringVar(root, value='') 
Faction = ["Blue", "Green", "Yellow", "Red"] 

#create a frame in the window 
app = Application(root) 

root.mainloop() 

回答

0

您需要confirm_button以捕捉combo box的選擇;沒有必要將組合框選擇綁定到捕獲;您也不需要將活動傳遞給firstfaction_onEnter;你可以直接使用combo_box來獲取選擇的值。

from tkinter import * 
from tkinter import ttk 

class Application(Frame): 

    def __init__(self, master): 

# Initialise the Frame 
     super(Application, self).__init__(master) 
     self.grid() 
     self.create_widgets() 

    def create_widgets(self): 

    #Define Combobox for Faction 
     self.firstfaction_ent = ttk.Combobox(self, textvariable=varSymbol, state='readonly') 
#  self.firstfaction_ent.bind("<Button-1>", self.firstfaction_onEnter)  
     self.firstfaction_ent['values'] = Faction 
     self.firstfaction_ent.grid(row=2, column=1) 

    #create a submit button 
     self.enter_bttn = Button(self, text="Confirm Your Results", command=self.firstfaction_onEnter).grid(row=7, column=1) 

    def firstfaction_onEnter(self): 
     Faction_First = self.firstfaction_ent.get() 
     print(Faction_First) 

#main 

root = Tk() 

#Window Title & size 
root.title("Sports Carnival Entry Window") 
root.geometry("600x500") 

varSymbol=StringVar(root, value='') 
varSecondFaction=StringVar(root, value='') 
Faction = ["Blue", "Green", "Yellow", "Red"] 

#create a frame in the window 
app = Application(root) 

root.mainloop()