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
|
1 Answer
Reset to default 0You 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
版权声明:本文标题:python - How to add a separator for CTK Text Box? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744750804a2623163.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
from tkinter import ...
:) – furas Commented Mar 12 at 15:18text != ""
insideadd_text_to_textbox()
will be evaluated asTrue
astext
has always a trailing newline character. – acw1668 Commented Mar 13 at 0:41