将ComboBox绑定到函数

时间:2016-10-16 12:55:26

标签: python-2.7 tkinter

我有一个选项菜单,我把它绑定到一个函数,但我不能把滚动条放在上面所以我使用ComboBox代替。但现在绑定不起作用

      Cat_options = ['Select Category']
      self.var = StringVar(self.ViewWM)
      conn = sqlite3.connect(self.DatabaseName)
      conn.text_factory = str
      cursor = conn.cursor()
      cursor.execute("SELECT Category FROM Websites_info WHERE LoginName LIKE ?" ,(self.LoginName,))
      conn.commit()
      a =''
      for row in cursor.fetchall():
          if a == row:
              pass
          else:
              row = str(row)
              row = row.replace("('","")
              row = row.replace("',)","")              
              Cat_options.append(row)
              a = row
      self.var.set(Cat_options[0]) # initial value
      Cat_ComboBox = ttk.Combobox(self.ViewWM, textvariable = self.var ,   values = Cat_options)
      Cat_ComboBox.place(x=10,y =45 , width = 183)
      Cat_ComboBox.bind('<<ComboBoxSelected>>', self.Cat_callback)
      b1 = Button(self.ViewWM,text="aaaa",command=self.Cat_callback)
      b1.place(x = 200,y=200)


  self.ViewWM.mainloop()
def Cat_callback(self, event=None):
    self.Selcted_Cat = self.var.get()
    print  self.Selcted_Cat
    print 'hello'

我的按钮工作正常但不绑定

2 个答案:

答案 0 :(得分:1)

问题在B中位于<<ComboBoxSelected>>上方。它必须是<<ComboboxSelected>>

完整的工作示例

from __future__ import print_function

try:
    # Python 2
    import Tkinter as tk
    import ttk
except ImportError:
    # Python 3
    import tkinter as tk
    from tkinter import ttk

# -------

class App(tk.Tk):

    def __init__(self):
        tk.Tk.__init__(self)

        self.var = tk.StringVar()

        options = ["Alpha", "Beta", "etc.", "Omega"]

        cb = ttk.Combobox(self, textvariable=self.var, values=options)
        cb.pack()
        cb.bind('<<ComboboxSelected>>', self.callback)

        b1 = ttk.Button(self, text="OK", command=self.callback)
        b1.pack()

    def callback(self, event=None):
        print('--- callback ---')
        print('var.get():', self.var.get())
        if event:
            print('event.widget.get():', event.widget.get())

# -------

App().mainloop()

答案 1 :(得分:0)

使用

     Cat_ComboBox.bind('<<ComboboxSelected>>',
                      lambda event: self.Cat_callback())

现在每件事都在工作

      Cat_options = ['Select Category']
      self.var = StringVar(self.ViewWM)
      conn = sqlite3.connect(self.DatabaseName)
      conn.text_factory = str
      cursor = conn.cursor()
      cursor.execute("SELECT Category FROM Websites_info WHERE LoginName LIKE ?" ,(self.LoginName,))
      conn.commit()
      a =''
      for row in cursor.fetchall():
          if a == row:
              pass
          else:
              row = str(row)
              row = row.replace("('","")
              row = row.replace("',)","")              
              Cat_options.append(row)
              a = row
      self.var.set(Cat_options[0]) # initial value
      Cat_ComboBox = ttk.Combobox(self.ViewWM, textvariable = self.var ,   values = Cat_options)
      Cat_ComboBox.place(x=10,y =45 , width = 183)
      Cat_ComboBox.bind('<<ComboboxSelected>>',
                      lambda event: self.Cat_callback())   #changing in code
      b1 = Button(self.ViewWM,text="aaaa",command=self.Cat_callback)
      b1.place(x = 200,y=200)


  self.ViewWM.mainloop()
def Cat_callback(self, event=None):
    self.Selcted_Cat = self.var.get()
    print  self.Selcted_Cat
    print 'hello'
相关问题