Skip to content

Fontchooser improvements #77

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 28 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
551a19c
Create NumberEntry widget
rdbende Mar 1, 2021
a6db0b3
Change the default hover-cursor to hand2
rdbende Mar 1, 2021
7611f2a
Change the movement cursor to fleur
rdbende Mar 1, 2021
61a46c5
Add NumberEntry to __init__.py
rdbende Mar 1, 2021
8941ab3
Fixed portability issues
rdbende Mar 2, 2021
8488995
Restore cursor
rdbende Mar 9, 2021
8b614d2
Restore linklabel
rdbende Mar 9, 2021
9584d3f
Add __getitem__, __setitem__ and configure
rdbende Mar 9, 2021
af42360
Update AUTHORS.md
rdbende Mar 9, 2021
a4af566
Create example for NumberEntry
rdbende Mar 9, 2021
439323e
Create unittest for NumberEntry
rdbende Mar 9, 2021
19f361e
Add NumberEntry to sphinx documentation
rdbende Mar 9, 2021
acdbec5
Update AUTHORS.md
rdbende Mar 9, 2021
fb2dd12
Fixed fatal config bug
rdbende Mar 11, 2021
4e83eda
Add files via upload
rdbende Mar 22, 2021
f8574af
Delete numberentry.py
rdbende Mar 22, 2021
95825ea
Delete test_numberentry.py
rdbende Mar 22, 2021
0d03190
Update ttkwidgets.rst
rdbende Mar 22, 2021
afd084d
Delete example_numberentry.py
rdbende Mar 22, 2021
63a54b8
Update __init__.py
rdbende Mar 22, 2021
af34b7f
Remove unnecessary font_indexes dict
rdbende Mar 22, 2021
7e6982e
Change 'is' to '=='
rdbende Mar 22, 2021
509ecb9
Change 'is' to '=='
rdbende Mar 22, 2021
9326114
Change 'is' to '=='
rdbende Mar 22, 2021
b5da217
Update AUTHORS.md
rdbende Mar 22, 2021
fb9bdfc
Update chooser.py
rdbende Mar 22, 2021
3f16478
Update chooser.py
rdbende Mar 22, 2021
e4b646c
No more missing listboxheight
rdbende Mar 22, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
115 changes: 65 additions & 50 deletions ttkwidgets/autocomplete/autocomplete_entrylistbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
Author: Juliette Monsel
License: GNU GPLv3
Source: This repository

Edited by rdbende: use <Return> as well to select the completition suggestion, and listboxheight option
to set the height of the listbox
"""
import tkinter as tk
from tkinter import ttk
Expand Down Expand Up @@ -33,13 +36,16 @@ def __init__(self, master=None, completevalues=[], allow_other_values=False,
:param font: font in entry and listbox
:param autohidescrollbar: whether to use an :class:`~ttkwidgets.AutoHideScrollbar` or a :class:`ttk.Scrollbar`
:type autohidescrollbar: bool
:param listboxheight: the height of the listbox given in rows
:type listboxheight: int
:param kwargs: keyword arguments passed to the :class:`ttk.Frame` initializer
"""
exportselection = kwargs.pop('exportselection', False)
width = kwargs.pop('width', None)
justify = kwargs.pop('justify', None)
font = kwargs.pop('font', None)
kwargs.setdefault('padding', 4)
exportselection = kwargs.pop("exportselection", False)
width = kwargs.pop("width", None)
justify = kwargs.pop("justify", None)
font = kwargs.pop("font", None)
self._listboxheight = kwargs.pop("listboxheight", 10)
kwargs.setdefault("padding", 4)

ttk.Frame.__init__(self, master, **kwargs)
self.columnconfigure(0, weight=1)
Expand All @@ -48,30 +54,33 @@ def __init__(self, master=None, completevalues=[], allow_other_values=False,
self._completevalues = completevalues
validatecmd = self.register(self._validate)
self.entry = ttk.Entry(self, width=width, justify=justify, font=font,
validate='key', exportselection=exportselection,
validate="key", exportselection=exportselection,
validatecommand=(validatecmd, "%d", "%S", "%i", "%s", "%P"))
f = ttk.Frame(self, style='border.TFrame', padding=1)
self.listbox = tk.Listbox(f, width=width, font=font,
f = ttk.Frame(self, style="border.TFrame", padding=1)
self.listbox = tk.Listbox(f, width=width, font=font, height=self._listboxheight,
exportselection=exportselection, selectmode="browse",
highlightthickness=0, relief='flat')
highlightthickness=0, relief="flat")
try:
self.listbox.configure(justify=justify) # this is an option only for tk >= 8.6.5
except tk.TclError:
pass
self.listbox.pack(fill='both', expand=True)
self.listbox.pack(fill="both", expand=True)
if autohidescrollbar:
self._scrollbar = AutoHideScrollbar(self, orient=tk.VERTICAL, command=self.listbox.yview)
else:
self._scrollbar = ttk.Scrollbar(self, orient=tk.VERTICAL, command=self.listbox.yview)
self.listbox.configure(yscrollcommand=self._scrollbar.set)
self.entry.grid(sticky='ew')
f.grid(sticky='nsew')
self._scrollbar.grid(row=1, column=1, sticky='ns')
self.entry.grid(sticky="ew")
f.grid(sticky="nsew")
self._scrollbar.grid(row=1, column=1, sticky="ns")
for c in self._completevalues:
self.listbox.insert('end', c)
self.listbox.insert(tk.END, c)

self.listbox.bind('<<ListboxSelect>>', self._update_entry)
self.listbox.bind("<<ListboxSelect>>", self._update_entry)
self.listbox.bind("<KeyPress>", self._listbox_keypress)
# Sometimes it’s annoying that you can’t select the completition
# with Enter, only with Tab, so I added that as well
self.entry.bind("<Return>", self._tab)
self.entry.bind("<Tab>", self._tab)
self.entry.bind("<Right>", self._right)
self.entry.bind("<Down>", self._down)
Expand All @@ -83,20 +92,21 @@ def __init__(self, master=None, completevalues=[], allow_other_values=False,

def _select_all(self, event):
"""Select all entry content."""
self.entry.selection_range(0, 'end')
self.entry.selection_range(0, tk.END)
return "break"

def _right(self, event):
"""Move at the end of selected text on right press."""
"""Move at the end of selected text on Right press."""
if self.entry.selection_present():
self.entry.select_clear()
self.entry.icursor("end")
self.entry.icursor(tk.END)
return "break"

def _tab(self, event):
"""Move at the end of selected text on tab press."""
"""Move at the end of selected text on Tab or Enter press."""
self.entry.select_clear()
self.entry.icursor("end")
self.entry.icursor(tk.END)
self.listbox.event_generate("<<ListboxSelect>>")
return "break"

def _listbox_keypress(self, event):
Expand All @@ -105,7 +115,7 @@ def _listbox_keypress(self, event):
l = [i for i in self._completevalues if i[0].lower() == key]
if l:
i = self._completevalues.index(l[0])
self.listbox.selection_clear(0, "end")
self.listbox.selection_clear(0, tk.END)
self.listbox.selection_set(i)
self.listbox.see(i)
self._update_entry()
Expand All @@ -114,44 +124,44 @@ def _up(self, event):
"""Navigate in the listbox with up key."""
try:
i = self.listbox.curselection()[0]
self.listbox.selection_clear(0, "end")
self.listbox.selection_clear(0, tk.END)
if i <= 0:
i = len(self._completevalues)
self.listbox.see(i - 1)
self.listbox.select_set(i - 1)
self.listbox.activate(i - 1)
except (tk.TclError, IndexError):
self.listbox.selection_clear(0, "end")
self.listbox.selection_clear(0, tk.END)
i = len(self._completevalues)
self.listbox.see(i - 1)
self.listbox.select_set(i - 1)
self.listbox.activate(i - 1)
self.listbox.event_generate('<<ListboxSelect>>')
self.listbox.event_generate("<<ListboxSelect>>")
return "break"

def _down(self, event):
"""Navigate in the listbox with down key."""
try:
i = self.listbox.curselection()[0]
self.listbox.selection_clear(0, "end")
self.listbox.selection_clear(0, tk.END)
if i >= len(self._completevalues) - 1:
i = -1
self.listbox.see(i + 1)
self.listbox.select_set(i + 1)
self.listbox.activate(i + 1)
except (tk.TclError, IndexError):
self.listbox.selection_clear(0, "end")
self.listbox.selection_clear(0, tk.END)
self.listbox.see(0)
self.listbox.select_set(0)
self.listbox.activate(0)
self.listbox.event_generate('<<ListboxSelect>>')
self.listbox.event_generate("<<ListboxSelect>>")
return "break"

def _validate(self, action, modif, pos, prev_txt, new_txt):
"""Complete the text in the entry with values."""
try:
sel = self.entry.selection_get()
txt = prev_txt.replace(sel, '')
txt = prev_txt.replace(sel, "")
except tk.TclError:
txt = prev_txt
if action == "0":
Expand All @@ -162,13 +172,13 @@ def _validate(self, action, modif, pos, prev_txt, new_txt):
l = [i for i in self._completevalues if i[:len(txt)] == txt]
if l:
i = self._completevalues.index(l[0])
self.listbox.selection_clear(0, "end")
self.listbox.selection_clear(0, tk.END)
self.listbox.selection_set(i)
self.listbox.see(i)
index = self.entry.index("insert")
self.entry.delete(0, "end")
self.entry.delete(0, tk.END)
self.entry.insert(0, l[0].replace("\ ", " "))
self.entry.selection_range(index + 1, "end")
self.entry.selection_range(index + 1, tk.END)
self.entry.icursor(index + 1)
return True
else:
Expand All @@ -186,30 +196,32 @@ def _update_entry(self, event=None):
sel = self.listbox.get(self.listbox.curselection()[0])
except (tk.TclError, IndexError):
return
self.entry.delete(0, "end")
self.entry.delete(0, tk.END)
self.entry.insert(0, sel)
self.entry.selection_clear()
self.entry.icursor("end")
self.event_generate('<<ItemSelect>>')
self.entry.icursor(tk.END)
self.event_generate("<<ItemSelect>>")

def keys(self):
keys = ttk.Frame.keys(self)
keys.extend(['completevalues', 'allow_other_values', 'exportselection',
'justify', 'font'])
keys.extend(["completevalues", "allow_other_values", "exportselection",
"justify", "font", "listboxheight"])
return keys

def get(self):
"Return the text in the entry."
return self.entry.get()

def cget(self, key):
if key == 'allow_other_values':
if key == "allow_other_values":
return self._allow_other_values
elif key == 'completevalues':
elif key == "completevalues":
return self._completevalues
elif key == 'autohidescrollbar':
elif key == "autohidescrollbar":
return isinstance(self._scrollbar, AutoHideScrollbar)
elif key in ['justify', 'font', 'exportselection', 'width']:
elif key == "listboxheight":
return self._listboxheight
elif key in ["justify", "font", "exportselection", "width"]:
return self.entry.cget(key)
else:
return ttk.Frame.cget(self, key)
Expand All @@ -219,39 +231,42 @@ def configure(self, cnf={}, **kw):
kwargs.update(cnf)
kwargs.update(kw)
# completion settings
self._allow_other_values = kwargs.pop('allow_other_values', self._allow_other_values)
if 'completevalues' in kwargs:
completevalues = kwargs.pop('completevalues')
self._allow_other_values = kwargs.pop("allow_other_values", self._allow_other_values)
if "completevalues" in kwargs:
completevalues = kwargs.pop("completevalues")
self._completevalues = completevalues
self.listbox.delete(0, 'end')
self.listbox.delete(0, tk.END)
for c in self._completevalues:
self.listbox.insert('end', c)
self.listbox.insert(tk.END, c)

# autohidescrollbar
autohidescrollbar = isinstance(self._scrollbar, AutoHideScrollbar)
autohidescrollbar2 = kwargs.pop('autohidescrollbar', autohidescrollbar)
autohidescrollbar2 = kwargs.pop("autohidescrollbar", autohidescrollbar)
if autohidescrollbar != autohidescrollbar2:
self._scrollbar.destroy()
if autohidescrollbar2:
self._scrollbar = AutoHideScrollbar(self, orient=tk.VERTICAL, command=self.listbox.yview)
else:
self._scrollbar = ttk.Scrollbar(self, orient=tk.VERTICAL, command=self.listbox.yview)
self.listbox.configure(yscrollcommand=self._scrollbar.set)
self._scrollbar.grid(row=1, column=1, sticky='ns')
self._scrollbar.grid(row=1, column=1, sticky="ns")
# entry/listbox settings
entry_listbox_kw = {}
for key in ['font', 'exportselection', 'width']:
for key in ["font", "exportselection", "width"]:
if key in kwargs:
entry_listbox_kw[key] = kwargs.pop(key)
self.entry.configure(entry_listbox_kw)
self.listbox.configure(entry_listbox_kw)
if 'justify' in kwargs:
justify = kwargs.pop('justify')
if "justify" in kwargs:
justify = kwargs.pop("justify")
self.entry.configure(justify=justify)
try:
self.listbox.configure(justify=justify) # this is an option only for tk >= 8.6.5
except tk.TclError:
pass
if "listboxheight" in kwargs:
self._listboxheight = kwargs.pop("listboxheight", "10")
self.listbox.configure(height=self._listboxheight)
# frame settings
ttk.Frame.config(self, kwargs)

Expand Down
Loading