Reputation: 79
I have an option menu and i bound it to a function but i can't put scrollbar on it so i used ComboBox instead. But now the binding doesn't work
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'
My a button works fine but doesn't bind
Upvotes: 2
Views: 16796
Reputation: 142641
Problem is upper B
in <<ComboBoxSelected>>
. It has to be <<ComboboxSelected>>
Full working example
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()
Upvotes: 1
Reputation: 79
with using
Cat_ComboBox.bind('<<ComboboxSelected>>',
lambda event: self.Cat_callback())
every thing its working now
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'
Upvotes: 1