I’m having quite a challenge trying to replicate an image within an entry box (see below)
The general prose is that I’d like to be able to change the blinking caret in an entry box to a different image / ASCII-Unicode symbol.
Could anyone give me a hand?
Simple (Code);
I’ve tried approaching this problem already using this script here;
import tkinter as tk
from tkinter import *
from PIL import ImageTk, Image
import os
# root
root=tk.Tk()
# constants
MAX_CHARS = 16
FONT_SIZE = 8
FONT="TkFixedFont"
COLOR="green2"
SCREEN_WIDTH = MAX_CHARS * FONT_SIZE * 2
SCREEN_HEIGHT = 200
# adjust window size
root.geometry(str(SCREEN_WIDTH)+"x"+str(SCREEN_HEIGHT))
root.minsize(SCREEN_WIDTH, SCREEN_HEIGHT)
root.maxsize(SCREEN_WIDTH, SCREEN_HEIGHT)
# console input
root.columnconfigure(1, weight=1, minsize=1)
root.columnconfigure(2, weight=0, minsize=1)
root.rowconfigure(1, weight=1, minsize=1)
# process input
def process(sv):
print('_n⬤n' + sv.get())
# update widget
def update(event, widget, sv, max):
# get stringvar
t = sv.get()
# get amount of letter
l = len(t)
# limit max letter
if l > max: sv.set(t[1:])
# process input on enter keypress
if (event.keysym == "Return"): process(sv)
# set background
frame = tk.Frame(root, bg='black')
frame.place(relwidth=1, relheight=1)
# create label
lt = '_n⬤'
label = Label(
root,
bg="black",
fg=COLOR,
highlightthickness=0,
borderwidth=0,
text=lt,
font=FONT
)
label.grid(row=2, column=0, sticky=SW)
# create listbox object
sv = StringVar()
entry = Entry(
root,
bg="black",
fg=COLOR,
highlightthickness=0,
borderwidth=0,
width=MAX_CHARS,
textvariable=sv,
insertontime = 0,
insertwidth=0,
font=FONT
)
entry.grid(row=2, column=1, sticky=SW)
entry.bind('<KeyRelease>', lambda e: update(e, entry, sv, MAX_CHARS))
# main loop
root.mainloop()
Basically all this does is shoves a label before an entry box and puts the “⬤” ascii beneath an “_” ascii with some additional extras (easy right?).
aaaaand whilst it does the job i want to a degree I’d like to add some flare or pisazz if you will. (what’s a project without a lil’ challenge ay’?)
Question(s) / Approach(es);
- (OPT 1) Can i change the caret image (“the blinking cursor” as it’s been described) in the entry box to an image or something resembling an over-score ( ̅ ) using tkinter?
- This would mean i could edit the label if the user types, remove the “fake over-score” and have it displayed above the cursors current position
(1. ̅● The quick brow –> 2. ● The quick br ̅o)
- This would mean i could edit the label if the user types, remove the “fake over-score” and have it displayed above the cursors current position
- (OPT 2) Can i change the caret image to the terminal cursor image? ( ̅●)
- (1. ̅● Hello! –> 2. He ̅●llo!)
- (OPT 3) Can i move the label over the entry box?
- (I would think this would be laggy and not very compatible with older machines but for sake of argument this is what it would look like 1. [ ̅●] Nonono –> 2. Non[ ̅●]ono)
- Can i fix the over-scores rendering issues somehow or should i use an image? ( ̅● <– notice how it displays to the left or right of a character not over the top or middle, and it isn’t “thick” enough and leaves an unwanted space)
- Do you recommend any other modules in python and approaches to this problem? (PYQT5?)
Extras;
As i want this to be portable to a RP-PI2(B+) (I’m probably going to use a different board later but this is just a demo for proof of concept) keep in mind that I’d like to avoid directly modifying the tkinters file for the caret image and find a more code-based solution but i welcome any answers regarding this still! It’s probably also worth noting I’m going to be trying to embed this GUI to replace the default RP-PI’s terminal at bootup)