#๐Ÿ”’ Customtkinter textbox help

23 messages ยท Page 1 of 1 (latest)

brittle oriole
#

when I click anywhere in the textbox, the cursor always goes to the first column and row. How can I make it so that the cursor goes to the same row where the mouse cursor is?

code:

import customtkinter
from customtkinter import CTkTextbox, CTkButton, CTkFrame

def center_window(window):
screen_width = window.winfo_screenwidth()
screen_height = window.winfo_screenheight()
window_width = 1000
window_height = 700

x = int((screen_width - window_width) / 2)
y = int((screen_height - window_height) / 2)

window.geometry(f"{window_width}x{window_height}+{x}+{y}")

app = customtkinter.CTk()
app.title("SuperCool NotePad")
app.geometry("1000x700")
app.minsize(500, 300)
app.grid_columnconfigure(0, weight=1)
app.grid_rowconfigure(1, weight=1)
customtkinter.set_appearance_mode("system")
customtkinter.set_default_color_theme("blue")

button_frame = CTkFrame(app)
button_frame.grid(row=0, column=0, sticky="ew", padx=4, pady=4)

button_save = CTkButton(button_frame, text="Save")
button_save.grid(row=0, column=0, padx=(4, 2), pady=4)

button_modifica = CTkButton(button_frame, text="Modifica")
button_modifica.grid(row=0, column=1, padx=2, pady=4)

textbox = CTkTextbox(app)
textbox.grid(row=1, column=0, sticky="nsew", padx=4, pady=4)

center_window(app)

app.mainloop()

lavish briarBOT
#

@brittle oriole

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

low gull
#

Hi, it's been so long since I've used customtkinter, but I don't remember such an option in it. At least to do it directly

#

Perhaps, you can create two textboxes and remove the space between them entirely? If you want them to appear close.

brittle oriole
#

hi, of course i can do that

low gull
#

Or you could fetch the mouse click position and calculate which row the user clicked, too

#

I believe there's a method for that

brittle oriole
#

i can try it

low gull
brittle oriole
#

thanks

#

this is for tkinter not for customtkinter, i can see if there's a similar solution

low gull
brittle oriole
#

there's a .cursor argument, i will try this way

low gull
#

Kk

brittle oriole
#

I HAVE DONE IT!!!

#

thx at ChatGPT

#

i will post the code so others can copy it and do the same

#

import customtkinter
import tkinter as tk

def center_window(window):
screen_width = window.winfo_screenwidth()
screen_height = window.winfo_screenheight()
window_width = 1000
window_height = 700
x = int((screen_width - window_width) / 2)
y = int((screen_height - window_height) / 2)
window.geometry(f"{window_width}x{window_height}+{x}+{y}")

def place_cursor(event):
font_size = 20
line_height = font_size + 8
clicked_line = event.y // line_height + 1
current_lines = int(textbox.index("end-1c").split('.')[0])
if clicked_line > current_lines:
textbox.insert("end", "\n" * (clicked_line - current_lines))
textbox.mark_set("insert", f"{clicked_line}.0")
textbox.focus()

app = customtkinter.CTk()
app.title("SuperCool NotePad")
app.geometry("1000x700")
app.minsize(500, 300)
app.grid_columnconfigure(0, weight=1)
app.grid_rowconfigure(1, weight=1)

customtkinter.set_appearance_mode("system")
customtkinter.set_default_color_theme("blue")

button_frame = customtkinter.CTkFrame(app)
button_frame.grid(row=0, column=0, sticky="ew", padx=4, pady=4)

button_save = customtkinter.CTkButton(button_frame, text="Save")
button_save.grid(row=0, column=0, padx=(4, 2), pady=4)

button_modifica = customtkinter.CTkButton(button_frame, text="Modifica")
button_modifica.grid(row=0, column=1, padx=2, pady=4)

textbox = customtkinter.CTkTextbox(app, wrap="none")
textbox.grid(row=1, column=0, sticky="nsew", padx=4, pady=4)
textbox.configure(font=("Roboto", 20))

textbox.bind("<Button-1>", place_cursor)

center_window(app)
app.mainloop()

lavish briarBOT
#

Hey @brittle oriole!

Please edit your message to use a code block

```py
print('Hello, world!')
```

This will result in the following:

print('Hello, world!')```
brittle oriole
#
import customtkinter
import tkinter as tk

def center_window(window):
    screen_width = window.winfo_screenwidth()
    screen_height = window.winfo_screenheight()
    window_width = 1000
    window_height = 700
    x = int((screen_width - window_width) / 2)
    y = int((screen_height - window_height) / 2)
    window.geometry(f"{window_width}x{window_height}+{x}+{y}")

def place_cursor(event):
    font_size = 20
    line_height = font_size + 8
    clicked_line = event.y // line_height + 1
    current_lines = int(textbox.index("end-1c").split('.')[0])
    if clicked_line > current_lines:
        textbox.insert("end", "\n" * (clicked_line - current_lines))
    textbox.mark_set("insert", f"{clicked_line}.0")
    textbox.focus()

app = customtkinter.CTk()
app.title("SuperCool NotePad")
app.geometry("1000x700")
app.minsize(500, 300)
app.grid_columnconfigure(0, weight=1)
app.grid_rowconfigure(1, weight=1)

customtkinter.set_appearance_mode("system")
customtkinter.set_default_color_theme("blue")

button_frame = customtkinter.CTkFrame(app)
button_frame.grid(row=0, column=0, sticky="ew", padx=4, pady=4)

button_save = customtkinter.CTkButton(button_frame, text="Save")
button_save.grid(row=0, column=0, padx=(4, 2), pady=4)

button_modifica = customtkinter.CTkButton(button_frame, text="Modifica")
button_modifica.grid(row=0, column=1, padx=2, pady=4)

textbox = customtkinter.CTkTextbox(app, wrap="none")
textbox.grid(row=1, column=0, sticky="nsew", padx=4, pady=4)
textbox.configure(font=("Roboto", 20))

textbox.bind("<Button-1>", place_cursor)

center_window(app)
app.mainloop()
#

perfect

lavish briarBOT
#
Python help channel closed for inactivity

This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.