如何在Python Tkinter中使用OptionMenu来设置文本框的对齐选项

3

我正在创建一个文字编辑器,希望在顶部设置一个任务栏,其中包含一个OptionMenu小部件,有三个可能的选择 - “右”,“左”和“中心”。当选择其中一个选项时,它应该获取该选项的值并使用.tag_add.insert.tag_config为每个值设置一个文本框窗口。下面是我的代码。所有代码都在名为Task1的框架内,文本框本身位于名为label_frame的框架内。接下来,任务栏和OptionMenu小部件位于名为Taskbar1的框架内。以下是完整的代码,可以使GUI工作。

from tkinter import *
from tkinter import ttk
from tkinter.scrolledtext import ScrolledText
import tkinter as tk
from tkinter import Menu, filedialog

root = Tk()

class ToDoList(tk.Frame):
    def __init__(self, master):
        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()                     
current_theme =style.theme_use()
style.theme_settings(current_theme, {"TNotebook.Tab": {"configure": {"padding": [20, 5], "background" : "white"}}})
style.theme_settings(current_theme, {"TNotebook" : {"configure" : {"tabposition" : "wn", "padding" : (0, 5)}}})
style.theme_settings(current_theme, {"TNotebook.Window" : {"configure" : {"width" : 500}}})

TasksList = ttk.Notebook(root)
Task1 = tk.Frame(TasksList, bg='white', height = 1000, width = 3000)

Taskbar1 = tk.Frame(Task1, bg="white", width=176)
Taskbar1.pack()
Button(Taskbar1, text="Hello", highlightthickness=0, borderwidth=0, highlightbackground = "white").pack(pady=[4, 5], padx=[3,3], ipadx = [2], ipady = [2], side = LEFT)


JustifyOptionList = ["right", "center", "left"]
JustifyDefaultOption=StringVar(Taskbar1)
JustifyDefaultOption.set(JustifyOptionList[0]) # default choice
JustifyOption= OptionMenu(Taskbar1, JustifyDefaultOption, *JustifyOptionList)
JustifyOption.pack(side = LEFT)

JustifyDefaultOption



entry1 = Entry(Task1, width = 60, font = "Calibri 20", highlightthickness = 0, justify = "center", selectborderwidth = 0, bd = 1, borderwidth = 0, relief = FLAT)
entry1.pack()

label_frame = tk.Frame(Task1, width=1000,height=550,bg="blue")
label_frame.pack()
label_frame.columnconfigure(0, weight=2)  
label_frame.rowconfigure(0, weight = 1)
label_frame.pack_propagate(0)



# create a Text widget
root.txt = tk.Text(label_frame)
root.txt.config(font=("TkMenuFont"), undo=True, wrap='word', highlightthickness=0, borderwidth=0, bd = 1, highlightbackground = "white", spacing1 = 5, spacing2 = 5, spacing3 = 5)
root.txt.tag_config(JustifyDefaultOption.get(), justify = JustifyDefaultOption.get())
root.txt.insert("1.0", "Please enter your notes here")
root.txt.tag_add(JustifyDefaultOption.get(), "1.0", "end")
root.txt.pack(expand=TRUE, fill = "both", side = LEFT)

# create a Scrollbar and associate it with txt
scrollb = tk.Scrollbar(label_frame, command=root.txt.yview, width = 16, bg = "white", troughcolor = "white", highlightbackground = "white")
scrollb.pack(fill = Y, side = RIGHT)
root.txt['yscrollcommand'] = scrollb.set






Task2 = tk.Frame(TasksList, bg='white')
text=ScrolledText(Task2, width = 176, height = 120, font = "TkMenuFont")
text.grid(row = 2, column = 0)
entry2 = Entry(Task2, width = 179, font = "TkMenuFont")
entry2.grid(row=0, column=0, sticky = W)


Task3 = tk.Frame(TasksList, bg = "white")
text=ScrolledText(Task3, width = 176, height = 120, font = "TkMenuFont")
text.grid(row = 2, column = 0)
entry3 = Entry(Task3, width = 179, font = "TkMenuFont")
entry3.grid(row=0, column=0, sticky = W)


TasksList.add(Task1,text = 'Click Here In Order To Read The Instructions')
TasksList.add(Task2, text = 'Two Two Two Two Two Two'[0: 40] + '...')
TasksList.add(Task3, text = "Three Three Three Three Three Three Three Extra"[0 : 40] + '...')
TasksList.grid(row=1, column=0, sticky=N+W, columnspan=3)


Button(root, text = "WELCOME", borderwidth=0, highlightthickness=0).grid(row=0, column=1, sticky=E, ipady = [5])



Label(text="HELLO", borderwidth=0, highlightthickness=0).grid(row=0, column=0, sticky=W, ipady = [5])

root.mainloop()

我对此感到困惑的部分是,尽管我有一个OptionList记录用户选择的选项,但即使我使用.get函数获取用户的对齐设置并将其应用于文本框,该选项也没有在对齐设置中设置。

1个回答

0
问题是每次选项更改时,您没有更改对齐设置。使用.get进行初始化不会在StringVar值更改时更新值。
将新的对齐设置应用于文本的一种方法是使用OptionMenucommand选项来实现:
import tkinter as tk

def justify_text(option):
    """Change text justify setting."""
    text.tag_configure('justify', justify=option)
    # make sure all text has the tag
    text.tag_add('justify', '1.0', 'end')

root = tk.Tk()

options = ["right", "center", "left"]
var = tk.StringVar(root, options[0])

menu = tk.OptionMenu(root, var, *options, command=justify_text)
menu.pack()

text = tk.Text(root)
text.insert('1.0',  "Please enter your notes here", 'justify')
text.tag_configure('justify', justify=options[0])

text.pack()

root.mainloop()

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接