admin管理员组

文章数量:1391929

I would like to add a separator line in between the entries of my text box but have not yet found a neat way of doing this.

I would also gladly appreciate any further comments on my code any ways to make it more readable etc.

import customtkinter as cTk


class MainWindow(cTk.CTk):
    def __init__(self):
        super().__init__()

        self.title("Separator Text Box Test")
        self.geometry("1280x600")

        self.text_box = cTk.CTkTextbox(self)
        self.text_box.place(relx=0.05, rely=0.07, relwidth=0.9, relheight=0.45)
        self.text_box.configure(state="disabled")

        # TESTING
        self.TestingFrame = cTk.CTkFrame(self)
        self.TestingFrame.place(relx=0.05, rely=0.55, relwidth=0.9, relheight=0.1)

        self.TestingEntry = cTk.CTkEntry(self.TestingFrame)
        self.TestingEntry.pack(side="left", fill="x", expand=True, padx=5, pady=5)

        self.TestingButton = cTk.CTkButton(self.TestingFrame, text="Add Text Field", command=self.add_text_to_textbox)
        self.TestingButton.pack(side="right", padx=5, pady=5)

        self.TestingDeleteButton = cTk.CTkButton(self, text="Delete All Entries", command=self.delete_all_entries)
        self.TestingDeleteButton.place(relx=0.05, rely=0.7, relwidth=0.4, relheight=0.05)

        self.TestingPrintButton = cTk.CTkButton(self, text="Print Latest Entry",
                                                command=self.print_latest_entry_from_textbox)
        self.TestingPrintButton.place(relx=0.55, rely=0.7, relwidth=0.4, relheight=0.05)

    def add_text_to_textbox(self):  # add separator using autoseparators maybe - dont actually know what that does
        text = self.TestingEntry.get() + "\n"
        if text != "":
            self.text_box.configure(state="normal")
            self.text_box.insert("0.0", text)
            self.text_box.configure(state="disabled")

    def delete_all_entries(self):
        self.text_box.configure(state="normal")
        self.text_box.delete("0.0", "end")
        self.text_box.configure(state="disabled")

    def print_latest_entry_from_textbox(self):
        text = self.text_box.get("1.0", "end-1c").split('\n')[0].strip()
        if text != "":
            print(f"Latest Entry: {text}")
        else:
            print("No entries available!")


if __name__ == "__main__":
    root = MainWindow()
    root.mainloop()

I would like to add a separator line in between the entries of my text box but have not yet found a neat way of doing this.

I would also gladly appreciate any further comments on my code any ways to make it more readable etc.

import customtkinter as cTk


class MainWindow(cTk.CTk):
    def __init__(self):
        super().__init__()

        self.title("Separator Text Box Test")
        self.geometry("1280x600")

        self.text_box = cTk.CTkTextbox(self)
        self.text_box.place(relx=0.05, rely=0.07, relwidth=0.9, relheight=0.45)
        self.text_box.configure(state="disabled")

        # TESTING
        self.TestingFrame = cTk.CTkFrame(self)
        self.TestingFrame.place(relx=0.05, rely=0.55, relwidth=0.9, relheight=0.1)

        self.TestingEntry = cTk.CTkEntry(self.TestingFrame)
        self.TestingEntry.pack(side="left", fill="x", expand=True, padx=5, pady=5)

        self.TestingButton = cTk.CTkButton(self.TestingFrame, text="Add Text Field", command=self.add_text_to_textbox)
        self.TestingButton.pack(side="right", padx=5, pady=5)

        self.TestingDeleteButton = cTk.CTkButton(self, text="Delete All Entries", command=self.delete_all_entries)
        self.TestingDeleteButton.place(relx=0.05, rely=0.7, relwidth=0.4, relheight=0.05)

        self.TestingPrintButton = cTk.CTkButton(self, text="Print Latest Entry",
                                                command=self.print_latest_entry_from_textbox)
        self.TestingPrintButton.place(relx=0.55, rely=0.7, relwidth=0.4, relheight=0.05)

    def add_text_to_textbox(self):  # add separator using autoseparators maybe - dont actually know what that does
        text = self.TestingEntry.get() + "\n"
        if text != "":
            self.text_box.configure(state="normal")
            self.text_box.insert("0.0", text)
            self.text_box.configure(state="disabled")

    def delete_all_entries(self):
        self.text_box.configure(state="normal")
        self.text_box.delete("0.0", "end")
        self.text_box.configure(state="disabled")

    def print_latest_entry_from_textbox(self):
        text = self.text_box.get("1.0", "end-1c").split('\n')[0].strip()
        if text != "":
            print(f"Latest Entry: {text}")
        else:
            print("No entries available!")


if __name__ == "__main__":
    root = MainWindow()
    root.mainloop()
Share Improve this question edited Mar 12 at 18:35 President James K. Polk 42.1k29 gold badges109 silver badges145 bronze badges asked Mar 12 at 13:01 Paul HinterbauerPaul Hinterbauer 133 bronze badges 5
  • do you mean something like ttk.Separator – furas Commented Mar 12 at 13:55
  • 1 Sadly my projects needs to be contained in Ctk so i cannot use a tkinter method. If there is any way that i am not aware of to use tkinter methods in ctk without actually importing tkinter i would love to hear how to do that. – Paul Hinterbauer Commented Mar 12 at 14:15
  • 1 see source code for CustomTkinter - it starts with from tkinter import ... :) – furas Commented Mar 12 at 15:18
  • It is not clear what "separator" means. Is it an empty line in the text box? Or a solid line (graphically)? Note also that text != "" inside add_text_to_textbox() will be evaluated as True as text has always a trailing newline character. – acw1668 Commented Mar 13 at 0:41
  • Thanks for the replies! I have tried to use the separator function from tk but am struggling to use it in cTk and as for the separator itself i want it to be a thin line between the entries of the text box so multiline entries can clearly be distinguished from other entries – Paul Hinterbauer Commented Mar 13 at 13:09
Add a comment  | 

1 Answer 1

Reset to default 0

You can use an instance of cTk.CTkFrame with few pixels in height as the separator and insert it into the text box using window_create(). However cTk.CTkFrame.window_create() does not do that, you need to call that function on the internal tkinter.Text widget (the instance variable _textbox) instead:

def add_text_to_textbox(self):
    text = self.TestingEntry.get().strip() # remove leading and trailing spaces
    if text != "":
        self.text_box.configure(state="normal")
        self.text_box.insert("1.0", text+"\n")
        # separator using a frame with 2 pixels in height
        # and around the width of the internal text widget as width
        sep = cTk.CTkFrame(self.text_box, width=self.text_box._textbox.winfo_width()-10, height=2, fg_color='gray25')
        # call window_create() on the internal text widget
        self.text_box._textbox.window_create("1.end", window=sep)
        self.text_box.configure(state="disabled")

Sample result:

本文标签: pythonHow to add a separator for CTK Text BoxStack Overflow