tkinter - joint focus of two foreign objects? - python

I'm trying to improve an implementation of Entry with autocomplete. Currently the code uses a StringVar with a trace to track changes and posts a Listbox placed right under the Entry. In order for the Listbox to draw properly over other widgets I had to set its master to be the root/toplevel window.
Now I wanted to make listbox close when the entry loses focus. The thing is, the entry loses focus when the user selects an item from the listbox as well.
FocusOut takes places first and this closes the listbox before the choice is even made.
Right now I have it solved by setting a delayed call (50ms) when FocusOut occurs but that solution isn't great.
Is there another way around this? maybe a way to make the listbox be considered 'a part of' the entry?
This is the code I'm currently using with the timed solution:
# Based on: http://code.activestate.com/recipes/580770-combobox-autocomplete/ by Miguel Martinez Lopez
# Modified to work with toplevel windows and some other adjustments
from tkinter import *
from tkinter.ttk import *
def autoscroll(sbar, first, last):
"""Hide and show scrollbar as needed."""
first, last = float(first), float(last)
if first <= 0 and last >= 1:
sbar.grid_remove()
else:
sbar.grid()
sbar.set(first, last)
class Combobox_Autocomplete(Entry, object):
def __init__(self, master, list_of_items=None, autocomplete_function=None, listbox_width=None, listbox_height=7,
ignorecase_match=True, startswith_match=True, vscrollbar=True, hscrollbar=True, **kwargs):
if hasattr(self, "autocomplete_function"):
if autocomplete_function is not None:
raise ValueError("Combobox_Autocomplete subclass has 'autocomplete_function' implemented")
else:
if autocomplete_function is not None:
self.autocomplete_function = autocomplete_function
else:
if list_of_items is None:
raise ValueError("If not guiven complete function, list_of_items can't be 'None'")
if not ignorecase_match:
if startswith_match:
def matches_function(entry_data, item):
return item.startswith(entry_data)
else:
def matches_function(entry_data, item):
return item in entry_data
self.autocomplete_function = lambda entry_data: [item for item in self.list_of_items if
matches_function(entry_data, item)]
else:
if startswith_match:
def matches_function(escaped_entry_data, item):
if re.match(escaped_entry_data, item, re.IGNORECASE):
return True
else:
return False
else:
def matches_function(escaped_entry_data, item):
if re.search(escaped_entry_data, item, re.IGNORECASE):
return True
else:
return False
def autocomplete_function(entry_data):
escaped_entry_data = re.escape(entry_data)
return [item for item in self.list_of_items if matches_function(escaped_entry_data, item)]
self.autocomplete_function = autocomplete_function
self._listbox_height = int(listbox_height)
self._listbox_width = listbox_width
self.list_of_items = list_of_items
self._use_vscrollbar = vscrollbar
self._use_hscrollbar = hscrollbar
kwargs.setdefault("background", "white")
if "textvariable" in kwargs:
self._entry_var = kwargs["textvariable"]
else:
self._entry_var = kwargs["textvariable"] = StringVar()
Entry.__init__(self, master, **kwargs)
self._trace_id = self._entry_var.trace('w', self._on_change_entry_var)
self._listbox = None
# self.bind("<Tab>", self._on_tab)
self.bind("<Control-space>",lambda event: self.post_listbox())
self.bind("<Up>", self._previous)
self.bind("<Down>", self._next)
self.bind('<Control-n>', self._next)
self.bind('<Control-p>', self._previous)
self.bind("<Return>", self._update_entry_from_listbox)
self.bind("<Escape>", lambda event: self.unpost_listbox())
# self.bind("<FocusOut>", lambda event: self._update_entry_from_listbox(event, False))
self.bind("<FocusOut>", lambda event: self.after(50, self._on_focus_out, event))
parent = self.master
while not isinstance(parent, (Toplevel, Tk)):
parent = parent.master
self.parent_window = parent
def _on_focus_out(self, event):
if self._listbox:
focused = self._listbox.focus_get()
if focused and self._listbox.winfo_name() in str(focused):
return
self.unpost_listbox()
# def _on_focus_out_list(self, event):
# focused = self.focus_get()
# if focused and str(focused).endswith(self.winfo_name()):
# return
# self.unpost_listbox()
# def _on_tab(self, event):
# self.post_listbox()
# return "break"
def _on_change_entry_var(self, name, index, mode):
entry_data = self._entry_var.get()
if entry_data == '':
self.unpost_listbox()
# self.focus()
else:
values = self.autocomplete_function(entry_data)
if values:
if self._listbox is None:
self._build_listbox(values)
else:
self._listbox.delete(0, END)
height = min(self._listbox_height, len(values))
self._listbox.configure(height=height)
for item in values:
self._listbox.insert(END, item)
else:
self.unpost_listbox()
self.focus()
def _build_listbox(self, values):
listbox_frame = Frame(self.parent_window, padding=0)
if self._listbox_width:
width = self._listbox_width
else:
width = self.winfo_width()
self._listbox = Listbox(listbox_frame, background="white", selectmode=SINGLE, activestyle="none",
exportselection=False)
self._listbox.grid(row=0, column=0, sticky='nsew', padx=0, pady=0)
self._listbox.bind("<ButtonRelease-1>", self._update_entry_from_listbox)
self._listbox.bind("<Return>", self._update_entry_from_listbox)
self._listbox.bind("<Escape>", lambda event: self.unpost_listbox())
# self._listbox.bind("<FocusOut>", lambda event: self.after(50, self._on_focus_out_list, event))
self._listbox.bind('<Control-n>', self._next)
self._listbox.bind('<Control-p>', self._previous)
if self._use_vscrollbar:
vbar = Scrollbar(listbox_frame, orient=VERTICAL, command=self._listbox.yview)
vbar.grid(row=0, column=1, sticky='ns')
self._listbox.configure(yscrollcommand=lambda f, l: autoscroll(vbar, f, l))
if self._use_hscrollbar:
hbar = Scrollbar(listbox_frame, orient=HORIZONTAL, command=self._listbox.xview)
hbar.grid(row=1, column=0, sticky='ew')
self._listbox.configure(xscrollcommand=lambda f, l: autoscroll(hbar, f, l))
listbox_frame.grid_columnconfigure(0, weight=1)
listbox_frame.grid_rowconfigure(0, weight=1)
listbox_frame.place(in_=self, x=0, y=self.winfo_height(), width=width)
height = min(self._listbox_height, len(values))
self._listbox.configure(height=height)
for item in values:
self._listbox.insert(END, item)
def post_listbox(self):
if self._listbox is not None: return
entry_data = self._entry_var.get()
if entry_data == '': return
values = self.autocomplete_function(entry_data)
if values:
self._build_listbox(values)
def unpost_listbox(self):
if self._listbox is not None:
self._listbox.master.destroy()
self._listbox = None
def get_value(self):
return self._entry_var.get()
def set_value(self, text, close_dialog=False):
self._set_var(text)
if close_dialog:
self.unpost_listbox()
self.icursor(END)
self.xview_moveto(1.0)
def _set_var(self, text):
self._entry_var.trace_vdelete("w", self._trace_id)
self._entry_var.set(text)
self._trace_id = self._entry_var.trace('w', self._on_change_entry_var)
def _update_entry_from_listbox(self, event, focus=True):
if self._listbox is not None:
current_selection = self._listbox.curselection()
if current_selection:
text = self._listbox.get(current_selection)
self._set_var(text)
self._listbox.master.destroy()
self._listbox = None
if focus:
self.focus()
self.icursor(END)
self.xview_moveto(1.0)
return "break"
def _previous(self, event):
if self._listbox is not None:
current_selection = self._listbox.curselection()
if len(current_selection) == 0:
self._listbox.selection_set(0)
self._listbox.activate(0)
else:
index = int(current_selection[0])
self._listbox.selection_clear(index)
if index == 0:
index = END
else:
index -= 1
self._listbox.see(index)
self._listbox.selection_set(first=index)
self._listbox.activate(index)
return "break"
def _next(self, event):
if self._listbox is not None:
current_selection = self._listbox.curselection()
if len(current_selection) == 0:
self._listbox.selection_set(0)
self._listbox.activate(0)
else:
index = int(current_selection[0])
self._listbox.selection_clear(index)
if index == self._listbox.size() - 1:
index = 0
else:
index += 1
self._listbox.see(index)
self._listbox.selection_set(index)
self._listbox.activate(index)
return "break"
if __name__ == '__main__':
list_of_items = ["Cordell Cannata", "Lacey Naples", "Zachery Manigault", "Regan Brunt", "Mario Hilgefort",
"Austin Phong", "Moises Saum", "Willy Neill", "Rosendo Sokoloff", "Salley Christenberry",
"Toby Schneller", "Angel Buchwald", "Nestor Criger", "Arie Jozwiak", "Nita Montelongo",
"Clemencia Okane", "Alison Scaggs", "Von Petrella", "Glennie Gurley", "Jamar Callender",
"Titus Wenrich", "Chadwick Liedtke", "Sharlene Yochum", "Leonida Mutchler", "Duane Pickett",
"Morton Brackins", "Ervin Trundy", "Antony Orwig", "Audrea Yutzy", "Michal Hepp",
"Annelle Hoadley", "Hank Wyman", "Mika Fernandez", "Elisa Legendre", "Sade Nicolson", "Jessie Yi",
"Forrest Mooneyhan", "Alvin Widell", "Lizette Ruppe", "Marguerita Pilarski", "Merna Argento",
"Jess Daquila", "Breann Bevans", "Melvin Guidry", "Jacelyn Vanleer", "Jerome Riendeau",
"Iraida Nyquist", "Micah Glantz", "Dorene Waldrip", "Fidel Garey", "Vertie Deady",
"Rosalinda Odegaard", "Chong Hayner", "Candida Palazzolo", "Bennie Faison", "Nova Bunkley",
"Francis Buckwalter", "Georgianne Espinal", "Karleen Dockins", "Hertha Lucus", "Ike Alberty",
"Deangelo Revelle", "Juli Gallup", "Wendie Eisner", "Khalilah Travers", "Rex Outman",
"Anabel King", "Lorelei Tardiff", "Pablo Berkey", "Mariel Tutino", "Leigh Marciano", "Ok Nadeau",
"Zachary Antrim", "Chun Matthew", "Golden Keniston", "Anthony Johson", "Rossana Ahlstrom",
"Amado Schluter", "Delila Lovelady", "Josef Belle", "Leif Negrete", "Alec Doss", "Darryl Stryker",
"Michael Cagley", "Sabina Alejo", "Delana Mewborn", "Aurelio Crouch", "Ashlie Shulman",
"Danielle Conlan", "Randal Donnell", "Rheba Anzalone", "Lilian Truax", "Weston Quarterman",
"Britt Brunt", "Leonie Corbett", "Monika Gamet", "Ingeborg Bello", "Angelique Zhang",
"Santiago Thibeau", "Eliseo Helmuth"]
root = Tk()
root.geometry("300x200")
root.columnconfigure(0, weight=1)
root.rowconfigure(0, weight=1)
frm_form = Frame(root)
frm_form.grid(row=0, column=0)
lbl_names = Label(frm_form, text='Name:')
lbl_names.grid(row=0, column=0, padx=5, pady=5)
entry_name = Combobox_Autocomplete(frm_form, list_of_items=list_of_items, startswith_match=False)
entry_name.grid(row=0, column=1, padx=5)
entry_name.focus()
lbl_names = Label(frm_form, text='Food:')
lbl_names.grid(row=1, column=0, padx=5, pady=5)
entry_food = Combobox_Autocomplete(frm_form, list_of_items=['Apples', 'Oranges', 'Avocados'], startswith_match=False)
entry_food.grid(row=1, column=1, padx=5)
root.mainloop()

Related

How to disable specific index/es or item/s in combobox?

I have a combobox and I need to disable some items/indexes. Here is my code.
months = ['january','feruary'.....'december']
cbMonth=ttk.Combobox(frame,values=months)
cbMonth.set(0)
cbMonth.place(relx=xDistance,rely=yDistance,relwidth=xWidth,relheight=yHeight)
cbMonth.update()
Let's say I want to disable or grayed out months of january,march and december or index 5,9,12. How to do it in Python. Something like;
cbMonth.Index[1].Enabled=false or
cbMonth.cget('january').Disable
There isn't a way to disable items in Combobox. But, if you still want to do so you will have to create a custom Combobox.
Here is a demo on how to create a custom Combobox. (you may add custom functionality if you want to).
import tkinter as tk
class CustomComboBox(tk.Frame):
def __init__(self, parent, values=[], validate='none', validatecommand='none',*args, **kwargs):
super().__init__(parent, *args, **kwargs)
self.disabled = []
self.values = values
self.listbox = None
self.scrollbar = None
self.entry = tk.Entry(self, borderwidth=0, background="white",relief="groove")
self.entry.configure(validate=validate, validatecommand=validatecommand)
self.entry.pack(expand=True, fill='both', side='left')
self.entry.bind('<FocusIn>', self._removeListView)
self.arrowButton = tk.Label(self, text='↓', background="white", width=2)
self.arrowButton.pack(side='right', fill='y')
self.arrowButton.bind('<1>', self._listView)
self.previousIndex = 0
self.arrowButton.bind('<Enter>', self._changeBackground)
self.arrowButton.bind('<Leave>', self._changeBackground)
self.bind('<Configure>', self._updatePos)
def _changeBackground(self, event):
if str(event.type)=='Enter':
self.arrowButton.config(bg='#b3d2ff', border=1)
self.entry.config(border=1)
elif str(event.type) == 'Leave':
self.arrowButton.config(bg='white', border=0)
self.entry.config(border=0)
def _listView(self, event=None):
if self.listbox is None:
self.frame = tk.Frame(background=self['bg'] ,width=self.entry['width']+2)
self.frame.lift()
self.frame.place(x=self.winfo_x(), y=self.winfo_y()+self.winfo_height()-2, width=self.winfo_width())
self.listbox = tk.Listbox(self.frame, width=self.entry['width'], border=1, relief="groove", activestyle='none')
self.listbox.focus()
self._addValues()
self.listbox.bind('<<ListboxSelect>>', self._checkDisabled)
self.listbox.bind("<Motion>", self._highlight)
self.listbox.bind("<Leave>", self._removeHighlight)
self.listbox.pack(side='left', expand=True, fill='both')
self._updateListHeight()
else:
self._removeListView()
def _updatePos(self, event):
if self.listbox:
self.frame.place_configure(x=self.winfo_x(), y=self.winfo_y()+self.winfo_height()-2, width=self.winfo_width())
def _updateListHeight(self):
if self.listbox:
if self.listbox.size()>5:
self.listbox.configure(height=5)
if self.scrollbar is None:
self.scrollbar = tk.Scrollbar(self.frame, command = self.listbox.yview)
self.scrollbar.pack(side='right', fill="y")
self.listbox.config(yscrollcommand = self.scrollbar.set)
else:
self.listbox.configure(height=len(self.values))
if self.scrollbar:
self.scrollbar.destroy()
self.scrollbar = None
def _removeListView(self, event=None):
if self.listbox:
self.frame.destroy()
self.listbox = None
self.scrollbar = None
def _addValues(self, remove=(False, 0)):
if self.listbox:
if remove[0]:
self.listbox.delete(remove[1])
else:
for index, element in enumerate(self.values):
self.listbox.insert(index, element)
self._updateDisabled()
self._updateListHeight()
def _highlight(self, event):
self.listbox.itemconfig(self.previousIndex, background='white')
self.previousIndex = self.listbox.nearest(event.y)
if self.previousIndex not in self.disabled:
self.listbox.itemconfig(self.previousIndex, background='#b3d2ff')
def _removeHighlight(self, event):
self.listbox.itemconfig(self.previousIndex, background='white')
def _checkDisabled(self, event):
selection = self.listbox.curselection()[0]
if selection in self.disabled:
self.listbox.selection_clear(selection)
return
else:
self.entry.delete(0, 'end')
self.entry.insert(0, self.listbox.get(selection))
self.entry.focus_set()
self.event_generate('<<ComboboxSelected>>', when='tail')
def _updateDisabled(self, enable=(False, 0)):
if self.listbox:
for x in self.disabled:
self.listbox.itemconfig(x, fg="gray")
if enable[0]:
self.listbox.itemconfig(enable[1], fg="black")
def setValues(self, values): # adds values pass a list
self.values = values
self._addValues()
def removeValue(self, index): # removes values pass index
try:
self.values.pop(index)
self._addValues(remove=(True, index))
except IndexError:
pass
def disable(self, index:list): # disables the values and given indexes
if index not in self.disabled:
self.disabled.extend(index)
self._updateDisabled()
def enable(self, index:int): # enables the values at the given index
try:
self.disabled.remove(index)
self._updateDisabled((True, index))
except ValueError:
pass
def getCurrent(self): # returns current text in the entrybox
return self.entry.get()
def setDefault(self, index): # sets default value pass index
self.entry.insert(0, self.values[index])
(do note that this will not fully function like a Combobox).
Usage:
import tkinter as tk
from tkinter import ttk
from Combo import CustomComboBox
def truth(event):
return event.isalpha() or event==""
def fun(event):
print(monthchoosen.getCurrent())
monthchoosen.enable(2)
window = tk.Tk()
window.geometry('350x250')
ttk.Label(window, text = "Select the Month :",
font = ("Times New Roman", 10)).pack()
monthchoosen = CustomComboBox(window, validate='key', validatecommand=(window.register(truth), '%P'))
monthchoosen.values = ['January', 'Febuary', 'March', 'April', 'May', 'June', 'July']
monthchoosen.setDefault(1)
monthchoosen.bind('<<ComboboxSelected>>', fun)
monthchoosen.disable([2, 4, 3])
monthchoosen.pack(expand=True, fill='x')
window.mainloop()
You can change the values displayed (but not grayed) with postcommand:
def f():
co['values'] = ['c', 'd']
co = ttk.Combobox(values=['a', 'b'], postcommand=f)

Tkinter how to catch this particular key event

Problem Description
I have an application in Tkinter that uses a Listbox that displays search results. When I press command + down arrow key, I am putting the focus from the search field to the first item in the Listbox. This is exactly how I want the behaviour but instead for just the down arrow.
However, I am already binding the down arrow to this Listbox by self.bind("<Down>", self.moveDown). I can not understand why command + down works while simply down (to which I literally bind'ed it to) does not. Specifically the result of pressing the down arrow is the following
While pressing command + down gives the intended result:
How can I let down behave just like command + down, and what is the reason why command is required at all?
Code snippets
def matches(fieldValue, acListEntry):
pattern = re.compile(re.escape(fieldValue) + '.*', re.IGNORECASE)
return re.match(pattern, acListEntry)
root = Tk()
img = ImageTk.PhotoImage(Image.open('imgs/giphy.gif'))
panel = Label(root, image=img)
panel.grid(row=1, column=0)
entry = AutocompleteEntry(autocompleteList, panel, root, matchesFunction=matches)
entry.grid(row=0, column=0)
root.mainloop()
With AutocompleteEntry being:
class AutocompleteEntry(Tkinter.Entry):
def __init__(self, autocompleteList, df, panel, rdi, *args, **kwargs):
self.df = df
self.product_row_lookup = {key:value for value, key in enumerate(autocompleteList)}
temp = df.columns.insert(0, 'Product_omschrijving')
temp = temp.insert(1, 'grams')
self.result_list = pd.DataFrame(columns=temp)
self.panel = panel
self.rdi = rdi
# self.bind('<Down>', self.handle_keyrelease)
# Listbox length
if 'listboxLength' in kwargs:
self.listboxLength = kwargs['listboxLength']
del kwargs['listboxLength']
else:
self.listboxLength = 8
# Custom matches function
if 'matchesFunction' in kwargs:
self.matchesFunction = kwargs['matchesFunction']
del kwargs['matchesFunction']
else:
def matches(fieldValue, acListEntry):
pattern = re.compile('.*' + re.escape(fieldValue) + '.*', re.IGNORECASE)
return re.match(pattern, acListEntry)
self.matchesFunction = matches
Entry.__init__(self, *args, **kwargs)
self.focus()
self.autocompleteList = autocompleteList
self.var = self["textvariable"]
if self.var == '':
self.var = self["textvariable"] = StringVar()
self.var.trace('w', self.changed)
self.bind("<Right>", self.selection)
self.bind("<Up>", self.moveUp)
self.bind("<Down>", self.moveDown)
self.bind("<Return>", self.selection)
self.listboxUp = False
self._digits = re.compile('\d')
def changed(self, name, index, mode):
if self.var.get() == '':
if self.listboxUp:
self.listbox.destroy()
self.listboxUp = False
else:
words = self.comparison()
if words:
if not self.listboxUp:
self.listbox = Listbox(width=self["width"], height=self.listboxLength)
self.listbox.bind("<Button-1>", self.selection)
self.listbox.bind("<Right>", self.selection)
self.listbox.bind("<Down>", self.moveDown)
self.listbox.bind("<Tab>", self.selection)
self.listbox.place(x=self.winfo_x(), y=self.winfo_y() + self.winfo_height())
self.listboxUp = True
self.listbox.delete(0, END)
for w in words:
self.listbox.insert(END, w)
else:
if self.listboxUp:
self.listbox.destroy()
self.listboxUp = False
else:
string = self.get()
if '.' in string:
write_to_file(self, string)
def contains_digits(self, d):
return bool(self._digits.search(d))
def selection(self, event):
if self.listboxUp:
string = self.listbox.get(ACTIVE)
self.var.set(string + ' ')
self.listbox.destroy()
self.listboxUp = False
self.icursor(END)
def moveDown(self, event):
self.focus()
if self.listboxUp:
if self.listbox.curselection() == ():
index = '0'
print "ok"
else:
index = self.listbox.curselection()[0]
print "blah"
if index != END:
self.listbox.selection_clear(first=index)
print "noo"
if index != '0':
index = str(int(index) + 1)
self.listbox.see(index) # Scroll!
self.listbox.selection_set(first=index)
self.listbox.activate(index)
else:
print "not up"
def comparison(self):
return [w for w in self.autocompleteList if self.matchesFunction(self.var.get(), w)]
Both command+down and down should produce the same output excepted that down also types question mark  onto the entry which made the last letter typed is the question mark box.
This is because pressing command, your computer checks the option menu to see if there's a shortcut with that key, if there isn't any, it will not do anything. While tkinter registered the down button as being pressed, so the event was triggered.
In contrast, With out pressing command, the Entry first displays the value of "down", which there isn't any, then executes the event binding, what you can do is, in the event, remove the last letter of the Entry. You can do so by self.delete(len(self.get())-1) in your event. Or add a return 'break' at the end of your event to prevent it from being typed.
Unfortunately, it's really hard to understand your real problem because you've posted too much unrelated code and not enough related code. It seems to me that what you're trying to accomplish is to let the user press the down or up arrow while an entry has focus, and have that cause the selection in a listbox to move down or up. Further, it seems that part of the problem is that you're seeing characters in the entry widget that you do not want to see when you press down or up.
If that is the problem, the solution is fairly simple. All you need to do is have your binding return the string "break" to prevent the default binding from being processed. It is the default binding that is inserting the character.
Here is an example. Run the example, and press up and down to move the selection of the listbox. I've left out all of the code related to autocomplete so you can focus on how the event binding works.
import Tkinter as tk
class Example(object):
def __init__(self):
self.root = tk.Tk()
self.entry = tk.Entry(self.root)
self.listbox = tk.Listbox(self.root, exportselection=False)
for i in range(30):
self.listbox.insert("end", "Item #%s" % i)
self.entry.pack(side="top", fill="x")
self.listbox.pack(side="top", fill="both", expand=True)
self.entry.bind("<Down>", self.handle_updown)
self.entry.bind("<Up>", self.handle_updown)
def start(self):
self.root.mainloop()
def handle_updown(self, event):
delta = -1 if event.keysym == "Up" else 1
curselection = self.listbox.curselection()
if len(curselection) == 0:
index = 0
else:
index = max(int(curselection[0]) + delta, 0)
self.listbox.selection_clear(0, "end")
self.listbox.selection_set(index, index)
return "break"
if __name__ == "__main__":
Example().start()
For a fairly thorough explanation of what happens when an event is triggered, see this answer: https://stackoverflow.com/a/11542200/7432
Again, leaving aside the autocomplete requirement, I came up with a solution that uses that standard available commands and events for Listbox and Scrollbar. <<ListboxSelect>> lets you capture changes in selection from any of the lists and align the others. In addition, the Scrollbar and Listbox callbacks are directed to a routing function that passes things on to all of the listboxes.
# updownmultilistbox.py
# 7/24/2020
#
# incorporates vsb to propagate scrolling across lists
#
import tkinter as tk
class Example(object):
def __init__(self):
self.root = tk.Tk()
self.listOfListboxes = []
# self.active_lb = None
self.vsb = tk.Scrollbar(orient='vertical', command=self.OnVsb)
self.vsb.pack(side='right', fill='y')
self.lb1 = tk.Listbox(self.root, exportselection=0,
selectmode= tk.SINGLE, yscrollcommand=self.vsb_set)
self.lb2 = tk.Listbox(self.root, exportselection=0,
selectmode=tk.SINGLE, yscrollcommand=self.vsb_set)
self.lb3 = tk.Listbox(self.root, exportselection=0,
selectmode=tk.SINGLE, yscrollcommand=self.vsb_set)
self.listOfListboxes.append(self.lb1)
self.listOfListboxes.append(self.lb2)
self.listOfListboxes.append(self.lb3)
for i in range(30):
self.lb1.insert("end", "lb1 Item #%s" % i)
self.lb2.insert("end", "lb2 Item #%s" % i)
self.lb3.insert("end", "lb3 Item #%s" % i)
self.lb1.pack(side="left", fill="both", expand=True)
self.lb2.pack(side="left", fill="both", expand=True)
self.lb3.pack(side="left", fill="both", expand=True)
for lb in self.listOfListboxes:
lb.bind('<<ListboxSelect>>', self.handle_select)
for lb in self.listOfListboxes:
lb.selection_set(0)
lb.activate(0)
self.listOfListboxes[0].focus_force()
def start(self):
self.root.title('updownmultilistbox')
self.root.mainloop()
def OnVsb(self, *args):
for lb in self.listOfListboxes:
lb.yview(*args)
def vsb_set(self, *args):
print ('vsb_set args: ', *args)
self.vsb.set(*args)
for lb in self.listOfListboxes:
lb.yview_moveto(args[0])
def handle_select(self, event):
# set evey list to the same selection
print ('select handler: ', event, event.widget.curselection())
# self.active_lb = event.widget
for lb in self.listOfListboxes:
if lb != event.widget:
lb.selection_clear(0, 'end') # have to avoid this for the current widget
lb.selection_set(event.widget.curselection())
lb.activate(event.widget.curselection())
if __name__ == "__main__":
Example().start()

How to incorporate the functionality of Calendar (CalendarWidget.py) into the main program (GUIframe.py)?

I have two programs, one that displays a calendar in which the user can select his month year wise and then see the calendar of that month.Second, is a program which has three frames and a button "Calendar". What do I do so that when the "Calendar" button of the 'GUIframe.py' program is clicked, the functionality of the 'CalendarWidget.py' is put to effect?
-->GUIFrame
from tkinter import *
root = Tk()
frame = Frame(root)
frame.pack()
def Bprint():
print("CLICKED")
def Bprint1():
print("Clicked Blue!")
bottomframe1=Frame(root)
bottomframe1.pack( side = BOTTOM )
bottomframe = Frame(root)
bottomframe.pack( side = BOTTOM )
redbutton = Button(frame, text="Red", fg="red")
redbutton.pack( side = LEFT)
greenbutton = Button(frame, text="Brown", fg="brown")
greenbutton.pack(side = RIGHT)
bluebutton = Button(frame, text="Blue", fg="blue", command=Bprint1)
bluebutton.pack(side = LEFT)
blackbutton = Button(bottomframe, text="Black", fg="black", command=Bprint)
blackbutton.pack( side = BOTTOM)
cal_b= Button(bottomframe1, text="Calendar", fg="black")
cal_b.pack(side=BOTTOM)
root.mainloop()
--->CalendarWidget.py
import calendar
import tkinter
import tkinter.font
from tkinter import ttk
def get_calendar(locale, fwday):
# instantiate proper calendar class
if locale is None:
return calendar.TextCalendar(fwday)
else:
return calendar.LocaleTextCalendar(fwday, locale)
class Calendar(ttk.Frame):
# XXX ToDo: cget and configure
datetime = calendar.datetime.datetime
timedelta = calendar.datetime.timedelta
def __init__(self, master=None, **kw):
"""
WIDGET-SPECIFIC OPTIONS
locale, firstweekday, year, month, selectbackground,
selectforeground
"""
# remove custom options from kw before initializating ttk.Frame
fwday = kw.pop('firstweekday', calendar.MONDAY)
year = kw.pop('year', self.datetime.now().year)
month = kw.pop('month', self.datetime.now().month)
locale = kw.pop('locale', None)
sel_bg = kw.pop('selectbackground', '#ecffc4')
sel_fg = kw.pop('selectforeground', '#05640e')
self._date = self.datetime(year, month, 1)
self._selection = None # no date selected
ttk.Frame.__init__(self, master, **kw)
self._cal = get_calendar(locale, fwday)
self.__setup_styles() # creates custom styles
self.__place_widgets() # pack/grid used widgets
self.__config_calendar() # adjust calendar columns and setup tags
# configure a canvas, and proper bindings, for selecting dates
self.__setup_selection(sel_bg, sel_fg)
# store items ids, used for insertion later
self._items = [self._calendar.insert('', 'end', values='')
for _ in range(6)]
# insert dates in the currently empty calendar
self._build_calendar()
# set the minimal size for the widget
#self._calendar.bind('<Map>', self.__minsize)
def __setitem__(self, item, value):
if item in ('year', 'month'):
raise AttributeError("attribute '%s' is not writeable" % item)
elif item == 'selectbackground':
self._canvas['background'] = value
elif item == 'selectforeground':
self._canvas.itemconfigure(self._canvas.text, item=value)
else:
ttk.Frame.__setitem__(self, item, value)
def __getitem__(self, item):
if item in ('year', 'month'):
return getattr(self._date, item)
elif item == 'selectbackground':
return self._canvas['background']
elif item == 'selectforeground':
return self._canvas.itemcget(self._canvas.text, 'fill')
else:
r = ttk.tclobjs_to_py({item: ttk.Frame.__getitem__(self, item)})
return r[item]
def __setup_styles(self):
# custom ttk styles
style = ttk.Style(self.master)
arrow_layout = lambda dir: (
[('Button.focus', {'children': [('Button.%sarrow' % dir, None)]})]
)
style.layout('L.TButton', arrow_layout('left'))
style.layout('R.TButton', arrow_layout('right'))
def __place_widgets(self):
# header frame and its widgets
hframe = ttk.Frame(self)
lbtn = ttk.Button(hframe, style='L.TButton', command=self._prev_month)
rbtn = ttk.Button(hframe, style='R.TButton', command=self._next_month)
self._header = ttk.Label(hframe, width=15, anchor='center')
# the calendar
#self._calendar = ttk.Treeview(show='', selectmode='none', height=7)
self._calendar = ttk.Treeview(self, show='', selectmode='none', height=7)
# pack the widgets
hframe.pack(in_=self, side='top', pady=4, anchor='center')
lbtn.grid(in_=hframe)
self._header.grid(in_=hframe, column=1, row=0, padx=12)
rbtn.grid(in_=hframe, column=2, row=0)
self._calendar.pack(in_=self, expand=1, fill='both', side='bottom')
def __config_calendar(self):
cols = self._cal.formatweekheader(3).split()
self._calendar['columns'] = cols
self._calendar.tag_configure('header', background='grey90')
self._calendar.insert('', 'end', values=cols, tag='header')
# adjust its columns width
font = tkinter.font.Font()
maxwidth = max(font.measure(col) for col in cols)
for col in cols:
self._calendar.column(col, width=maxwidth, minwidth=maxwidth,
anchor='e')
def __setup_selection(self, sel_bg, sel_fg):
self._font = tkinter.font.Font()
self._canvas = canvas = tkinter.Canvas(self._calendar,
background=sel_bg, borderwidth=0, highlightthickness=0)
canvas.text = canvas.create_text(0, 0, fill=sel_fg, anchor='w')
canvas.bind('<ButtonPress-1>', lambda evt: canvas.place_forget())
self._calendar.bind('<Configure>', lambda evt: canvas.place_forget())
self._calendar.bind('<ButtonPress-1>', self._pressed)
#def __minsize(self, evt):
# width, height = self._calendar.master.geometry().split('x')
# height = height[:height.index('+')]
# self._calendar.master.minsize(width, height)
def _build_calendar(self):
year, month = self._date.year, self._date.month
# update header text (Month, YEAR)
header = self._cal.formatmonthname(year, month, 0)
self._header['text'] = header.title()
# update calendar shown dates
cal = self._cal.monthdayscalendar(year, month)
for indx, item in enumerate(self._items):
week = cal[indx] if indx < len(cal) else []
fmt_week = [('%02d' % day) if day else '' for day in week]
self._calendar.item(item, values=fmt_week)
def _show_selection(self, text, bbox):
"""Configure canvas for a new selection."""
x, y, width, height = bbox
textw = self._font.measure(text)
canvas = self._canvas
canvas.configure(width=width, height=height)
canvas.coords(canvas.text, width - textw, height / 2 - 1)
canvas.itemconfigure(canvas.text, text=text)
canvas.place(in_=self._calendar, x=x, y=y)
# Callbacks
def _pressed(self, evt):
"""Clicked somewhere in the calendar."""
x, y, widget = evt.x, evt.y, evt.widget
item = widget.identify_row(y)
column = widget.identify_column(x)
if not column or not item in self._items:
# clicked in the weekdays row or just outside the columns
return
item_values = widget.item(item)['values']
if not len(item_values): # row is empty for this month
return
text = item_values[int(column[1]) - 1]
if not text: # date is empty
return
bbox = widget.bbox(item, column)
if not bbox: # calendar not visible yet
return
# update and then show selection
text = '%02d' % text
self._selection = (text, item, column)
self._show_selection(text, bbox)
def _prev_month(self):
"""Updated calendar to show the previous month."""
self._canvas.place_forget()
self._date = self._date - self.timedelta(days=1)
self._date = self.datetime(self._date.year, self._date.month, 1)
self._build_calendar() # reconstuct calendar
def _next_month(self):
"""Update calendar to show the next month."""
self._canvas.place_forget()
year, month = self._date.year, self._date.month
self._date = self._date + self.timedelta(
days=calendar.monthrange(year, month)[1] + 1)
self._date = self.datetime(self._date.year, self._date.month, 1)
self._build_calendar() # reconstruct calendar
# Properties
#property
def selection(self):
"""Return a datetime representing the current selected date."""
if not self._selection:
return None
year, month = self._date.year, self._date.month
return self.datetime(year, month, int(self._selection[0]))
def test():
import sys
root = tkinter.Tk()
root.title('Ttk Calendar')
ttkcal = Calendar(firstweekday=calendar.SUNDAY)
ttkcal.pack(expand=1, fill='both')
if 'win' not in sys.platform:
style = ttk.Style()
style.theme_use('clam')
def print_date():
print ("the date is:", ttkcal.selection)
tkinter.Button(root, text="Print it!", command=print_date).pack()
root.mainloop()
print(ttkcal.selection, "is your selection")
if __name__ == '__main__':
test()
You need to bind a command to the button from your file it looks like you want to call test so you can just do from CalendarWidget import test and then do
cal_b= Button(bottomframe1, text="Calendar", fg="black", command = test)
Although you probably don't want extra Tk() instances all over the place, so you should use Toplevel widget for your calendar.

Why is the tk key binding not working here?

Here is the code:
#!/usr/bin/env python3
################################################################
### Solve the unknown
################################################################
class App:
def __init__(self, root):
self.fields = ["A", "B", "AB"]
self.root = root
self.entries = []
self.numerics = []
self.makeRows()
Button(self.root, text="Run", command=self.run).grid(row=3, column=0)
Button(self.root, text="Reset", command=self.reset).grid(row=3, column=1)
root.bind("<Return>", self.run)
root.bind("<space>", self.reset)
def makeRows(self):
for i in range(len(self.fields)):
label = Label(self.root, text=self.fields[i], justify=LEFT)
entry = Entry(self.root)
label.grid(row=i, sticky=W)
entry.grid(row=i, column=1)
self.entries.append(entry)
def getValues(self):
try:
values = [e.get() for e in self.entries]
self.numerics = [float(v) if v!="" else v for v in values]
print(self.numerics)
except ValueError:
messagebox.showerror(title="Error", message="Input only numerical values!")
self.numerics = []
def checkEmpty(self, elem):
if elem == "":
return 1
else:
return 0
def checkEmptyInEntries(self):
if len(self.numerics) != 0:
entry_emptiness = [self.checkEmpty(v) for v in self.numerics]
if sum(entry_emptiness) != 1:
messagebox.showerror(title="Error", message="Leave one and only one entry empty!")
return None
return entry_emptiness
else:
return None
def run(self):
self.getValues()
emptiness = self.checkEmptyInEntries()
if emptiness == None:
return None
empty_index = emptiness.index(1)
if empty_index == 0:
self.entries[0].delete(0, END)
self.entries[0].insert(0, str(self.numerics[2] / self.numerics[1]))
elif empty_index == 1:
self.entries[1].delete(0, END)
self.entries[1].insert(0, str(self.numerics[2] / self.numerics[0]))
else:
self.entries[2].delete(0, END)
self.entries[2].insert(0, str(self.numerics[0] * self.numerics[1]))
def reset(self):
for entry in self.entries:
entry.delete(0, END)
from tkinter import *
root = Tk()
app = App(root)
root.mainloop()
The buttons work fine, but whenever I press enter, Python complains:
TypeError: run() takes 1 positional argument but 2 were given
Using bind to bind events to callbacks causes an event object to be sent to the callback every time the binding is invoked. You need to handle this extra argument. The easiest way to do this is to use a lambda function:
root.bind("<Return>", lambda _: self.run())
In the above code, the _ will be the event object.
Note that you could also change the definition of run to accept this object:
def run(self, event):
But I personally prefer the lambda because it makes it clearer that run does not use the event object.

i cant use the equals or the enter key on my keyboard to get an answer for anything [duplicate]

This question already has answers here:
python tkinter how to bind key to a button
(3 answers)
Closed 4 months ago.
ok i am using this code for educational purposes and if i go 45+54 and hit enter key i dont get any answer but if use the = on the screen not on my keyboard it works. i am so stuck and stressed out because i have done so much research but not able to find my answer. all i ask for the help is that am i missing a piece of code that is stopping me to use the equals key or the enter key on the numpad. please help here this is the code:
from tkinter import *
import tkinter
# Calculator is a class derived from Frame. Frames, being someone generic,
# make a nice base class for whatever you what to create.
class Calculator(Frame):
# Create and return a packed frame.
def frame(this, side):
w = Frame(this)
w.pack(side=side, expand=YES, fill=BOTH)
return w
# Create and return a button.
def button(this, root, side, text, command=None):
w = Button(root, text=text, command=command)
w.pack(side=side, expand=YES, fill=BOTH)
return w
# Enter a digit.
need_clr = False
def digit(self, digit):
if self.need_clr:
self.display.set('')
self.need_clr = False
self.display.set(self.display.get() + digit)
# Change sign.
def sign(self):
need_clr = False
cont = self.display.get()
if len(cont) > 0 and cont[0] == '-':
self.display.set(cont[1:])
else:
self.display.set('-' + cont)
# Decimal
def decimal(self):
self.need_clr = False
cont = self.display.get()
lastsp = cont.rfind(' ')
if lastsp == -1:
lastsp = 0
if cont.find('.',lastsp) == -1:
self.display.set(cont + '.')
# Push a function button.
def oper(self, op):
self.display.set(self.display.get() + ' ' + op + ' ')
self.need_clr = False
# Calculate the expressoin and set the result.
def calc(self):
try:
self.display.set(eval(self.display.get()))
self.need_clr = True
except:
showerror('Operation Error', 'Illegal Operation')
self.display.set('')
self.need_clr = False
def Enter(self):
self.display.set('Enter')
def keyPressed(self,event):
if event.keysym == 'Enter':
self.enter()
def __init__(self):
Frame.__init__(self)
self.option_add('*Font', 'Verdana 12 bold')
self.pack(expand=YES, fill=BOTH)
self.master.title('Simple Calculator')
# The StringVar() object holds the value of the Entry.
self.display = StringVar()
e = Entry(self, relief=SUNKEN, textvariable=self.display)
e.pack(side=TOP, expand=YES, fill=BOTH)
# This is a nice loop to produce the number buttons. The Lambda
# is an anonymous function.
for key in ("123", "456", "789"):
keyF = self.frame(TOP)
for char in key:
self.button(keyF, LEFT, char,
lambda c=char: self.digit(c))
keyF = self.frame(TOP)
self.button(keyF, LEFT, '-', self.sign)
self.button(keyF, LEFT, '0', lambda ch='0': self.digit(ch))
self.button(keyF, LEFT, '.', self.decimal)
# The frame is used to hold the operator buttons.
opsF = self.frame(TOP)
for char in "+-*/=":
if char == '=':
btn = self.button(opsF, LEFT, char, self.calc)
else:
btn = self.button(opsF, LEFT, char,
lambda w=self, s=char: w.oper(s))
# Clear button.
clearF = self.frame(BOTTOM)
self.Enter_button = self.button(clearF, LEFT, 'Enter', self.Enter)
self.bind_all('<Key>', self.keyPressed)
# Make a new function for the - sign. Maybe for . as well. Add event
# bindings for digits to call the button functions.
# This allows the file to be used either as a module or an independent
# program.
if __name__ == '__main__':
Calculator().mainloop()
To call a the same function as a button press on a keypress you have to bind keypress to you GUI.
Example
try:
# for Python2
from Tkinter import *
except ImportError:
# for Python3
from tkinter import *
class GUI(Frame):
# Create and return a packed frame.
def frame(self, side):
w = Frame(self)
w.pack(side=side, expand=YES, fill=BOTH)
return w
# Create and return a button.
def button(self, root, side, text, command=None):
w = Button(root, text=text, command=command)
w.pack(side=side, expand=YES, fill=BOTH)
return w
def hello(self):
self.display.set('hello')
def keyPressed(self,event):
#test to see whether enetr is pressed
if event.keysym == 'Return':
self.hello()
def __init__(self):
Frame.__init__(self)
self.option_add('*Font', 'Verdana 12 bold')
self.pack(expand=YES, fill=BOTH)
self.master.title('Simple GUI')
# The StringVar() object holds the value of the Entry.
self.display = StringVar()
e = Entry(self, relief=SUNKEN, textvariable=self.display)
e.pack(side=TOP, expand=YES, fill=BOTH)
# Clear button.
clearF = self.frame(BOTTOM)
self.hello_button = self.button(clearF, LEFT, 'Hello', self.hello)
#bind keypresses
self.bind_all('<Key>', self.keyPressed)
if __name__ == '__main__':
GUI().mainloop()
for this specific example the folowing code shoulod work:
try:
# for Python2
from Tkinter import *
except ImportError:
# for Python3
from tkinter import *
# Calculator is a class derived from Frame. Frames, being someone generic,
# make a nice base class for whatever you what to create.
# Calculator is a class derived from Frame. Frames, being someone generic,
# make a nice base class for whatever you what to create.
class Calculator(Frame):
# Create and return a packed frame.
def frame(this, side):
w = Frame(this)
w.pack(side=side, expand=YES, fill=BOTH)
return w
def keyPressed(self,event):
#test to see whether enetr is pressed
if event.keysym == 'Return':
self.calc()
# Create and return a button.
def button(this, root, side, text, command=None):
w = Button(root, text=text, command=command)
w.pack(side=side, expand=YES, fill=BOTH)
return w
# Enter a digit.
need_clr = False
def digit(self, digit):
if self.need_clr:
self.display.set('')
self.need_clr = False
self.display.set(self.display.get() + digit)
# Change sign.
def sign(self):
need_clr = False
cont = self.display.get()
if len(cont) > 0 and cont[0] == '-':
self.display.set(cont[1:])
else:
self.display.set('-' + cont)
# Decimal
def decimal(self):
self.need_clr = False
cont = self.display.get()
lastsp = cont.rfind(' ')
if lastsp == -1:
lastsp = 0
if cont.find('.',lastsp) == -1:
self.display.set(cont + '.')
# Push a function button.
def oper(self, op):
self.display.set(self.display.get() + ' ' + op + ' ')
self.need_clr = False
# Calculate the expressoin and set the result.
def calc(self):
try:
self.display.set(eval(self.display.get()))
self.need_clr = True
except:
showerror('Operation Error', 'Illegal Operation')
self.display.set('')
self.need_clr = False
def __init__(self):
Frame.__init__(self)
self.option_add('*Font', 'Verdana 12 bold')
self.pack(expand=YES, fill=BOTH)
self.master.title('Simple Calculator')
# The StringVar() object holds the value of the Entry.
self.display = StringVar()
e = Entry(self, relief=SUNKEN, textvariable=self.display)
e.pack(side=TOP, expand=YES, fill=BOTH)
# This is a nice loop to produce the number buttons. The Lambda
# is an anonymous function.
for key in ("123", "456", "789"):
keyF = self.frame(TOP)
for char in key:
self.button(keyF, LEFT, char,
lambda c=char: self.digit(c))
keyF = self.frame(TOP)
self.button(keyF, LEFT, '-', self.sign)
self.button(keyF, LEFT, '0', lambda ch='0': self.digit(ch))
self.button(keyF, LEFT, '.', self.decimal)
# The frame is used to hold the operator buttons.
opsF = self.frame(TOP)
for char in "+-*/=":
if char == '=':
btn = self.button(opsF, LEFT, char, self.calc)
else:
btn = self.button(opsF, LEFT, char,
lambda w=self, s=char: w.oper(s))
# Clear button.
clearF = self.frame(BOTTOM)
self.button(clearF, LEFT, 'Clr', lambda w=self.display: w.set(''))
self.bind_all('<Key>', self.keyPressed)
# Make a new function for the - sign. Maybe for . as well. Add event
# bindings for digits to call the button functions.
# This allows the file to be used either as a module or an independent
# program.
if __name__ == '__main__':
Calculator().mainloop()

Categories

Resources