【问题标题】:How Do I Wrap Text In A Tkinter Python Notebook Tab And Keep The Width Of Tkinter Notebook Tab Constant如何在 Tkinter Python Notebook 选项卡中换行文本并保持 Tkinter Notebook 选项卡的宽度不变
【发布时间】:2018-07-12 14:34:49
【问题描述】:

我真的有一个非常简单的问题。所以我正在制作一个待办事项列表,它需要一个 Tkinter Notebook 元素,该元素的一侧有标签,带有人们笔记的标题和日期,右侧是用户存储笔记的文本编辑器.

但是,我无法为每个选项卡设置相同的宽度和高度。选项卡的大小会根据用户的注释标题不断增加和减小,但我想要发生的是,当标题的文本超过选项卡宽度的大小时,只有一些标题出现和消失。否则,我想要发生的是让文本换行并增加高度(但绝不是宽度)。

在下面的代码中,标题是 one one onetwo two twothree three three 文本。我尝试使用.grid_propagate(FALSE),但没有成功。

from tkinter import *
import datetime
from tkinter import ttk
from tkinter.scrolledtext import ScrolledText
import tkinter as tk
root = Tk()
root.columnconfigure(2, weight=1)
root.rowconfigure(1, weight=1)


root.title("To - Do List")
root.geometry("1200x600")
root.configure(background = "white")
# Variable list:

style = ttk.Style()
style.configure(root, tabposition = "wn")

TasksList = ttk.Notebook(root)

Task1 = tk.Frame(TasksList, bg='white', width=600, height=500)
TasksList.add(Task1,text = 'One One One One One One')
Task1.grid_propagate(FALSE)

Task2 = tk.Frame(TasksList, bg='white', width=600, height=200)
TasksList.add(Task2, text = 'Two Two Two Two Two Two')
Task2.grid_propagate(FALSE)

Task3 = tk.Frame(TasksList, bg = "white", width = 600, height = 200)
TasksList.add(Task3, text = "Three Three Three Three Three Three Three")
Task3.grid_propagate(FALSE)

【问题讨论】:

    标签: python-3.x tkinter


    【解决方案1】:

    我不认为你可以对标签的宽度做任何事情。鉴于您要实现的设计,最简单的解决方案是不使用笔记本小部件。相反,只需使用一个带有列表框的文本小部件来显示笔记列表。

    在以下示例中,注释存储在字典 (self.notes) 中,键是注释标题。为简单起见,这不处理具有相同标题的笔记的情况,并且在不排序的情况下附加新笔记。它也仅在您切换笔记时保存笔记。

    import tkinter as tk
    
    class Example():
        def __init__(self):
            root = tk.Tk()
            self.lb = tk.Listbox(root, width=10)
            self.text = tk.Text(root)
    
            self.lb.pack(side="left", fill="y")
            self.text.pack(side="right", fill="both", expand=True)
    
            self.current = None
            self.notes = {}
    
            self.lb.bind("<<ListboxSelect>>", self.show_item)
    
        def show_item(self, event=None):
            selection = self.lb.curselection()
            if selection:
                index = selection[0]
                title = self.lb.get(index)
    
                # save current note
                if self.current:
                    self.notes[self.current] = self.text.get("1.0", "end-1c")
    
                # show new item
                self.current = title
                self.text.delete("1.0", "end")
                self.text.insert("1.0", self.notes[title])
    
    
        def add_note(self, title, body):
            self.notes[title] = body
            self.lb.insert("end", title)
    
    example = Example()
    example.add_note("note one", "this is the body of note one")
    example.add_note("Note with a really long title", 
                     "this is the body of the note with a long title")
    example.add_note("x", "this is the body of note x")
    
    tk.mainloop()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-05
      • 2018-04-05
      • 1970-01-01
      • 1970-01-01
      • 2019-08-12
      相关资源
      最近更新 更多