Jun-30-2025, 04:19 PM
for index = "10.5"
why is textbox.compare(index,"==","10.end") TRUE for all values beyond 10.4
I would like to test (textbox.compare) where the end point is to place a string in a textbox at a certain location.
If a given index is to the right of the end of the line -> pad from end to index with spaces then write the string.
If exactly at the end of the line just write (textbox.insert) the string.
If to the left of the end of the line -> delete (textbox.delete) from index to end then write the string.
I would like to understand the correct way to test for the position...this illustrates my issue:
dmac6809
why is textbox.compare(index,"==","10.end") TRUE for all values beyond 10.4
I would like to test (textbox.compare) where the end point is to place a string in a textbox at a certain location.
If a given index is to the right of the end of the line -> pad from end to index with spaces then write the string.
If exactly at the end of the line just write (textbox.insert) the string.
If to the left of the end of the line -> delete (textbox.delete) from index to end then write the string.
I would like to understand the correct way to test for the position...this illustrates my issue:
import tkinter as tk
from tkinter import ttk
# create - main parent
window=tk.Tk()
#window attributes
window.title("Testing")
window.attributes("-fullscreen", True)#no top bar on window so can't resize or close window - button to exit
window.config(background="black")
#create Widget
textbox= tk.Text(master=window,
height=24,#number of lines on screen
width=100,#number of characters per line
border=1,#just so I can see the box edges
borderwidth=2,#just so I can see the box edges
font=("Lucinda",24),
background="black",
foreground="#55dddd")
textbox.pack()
def init_text_window():#writes a new line character for 24 lines on screen - change if textbox height changes
for _ in range(0,24):
textbox.insert("0.0","\n")
test_button=ttk.Button(master=window,
text="exit program",
command=lambda: window.quit())
test_button.pack()
init_text_window()#done first - places a \n at the end of each line of screen
for i in range (1,10):
textbox.insert(f"{i}.0","TEST test TEST test")#nonsense text for testing
test_index=textbox.index("8.end")
print(test_index)
for i in range (15,30):
if textbox.compare(test_index,">",f"8.{i}"):
print(f"{test_index} tests greater than 8.{i}")
if textbox.compare(test_index,"==",f"8.{i}"):
print(f"{test_index} test equal to 8.{i}")
if textbox.compare(test_index,"<",f"8.{i}"):
print(f"{test_index} tests less than 8.{i}")
#run
window.mainloop()printed to output screen on VSCode dmac6809
