输入Tkinter后的Recenter文本(Python)

时间:2016-08-02 17:18:11

标签: python tkinter

我现在正在使用Tkinter for Python,我试图弄清楚在用户向Text小部件输入内容后是否可以重新输入文本。

目前,我的代码输出如下:

The program when it starts. The program after a user inputs information.

我想要的是输入的文本在文本字段中居中显示。我的代码如下。所有文本条目基本相同,因此大多数代码都是相似的。我只是为了全面而包含它,但我很确定它只需要处理修改Text(...)对象。

谢谢。

root = Tk()
root.title("Generate Report")

bdFrame = ttk.Frame(root, padding="3 3 12 12", relief="groove", borderwidth=.5)
bdFrame.grid(column=0, row=0, rowspan = 7,sticky=(N, W, E, S))

vrFrame = ttk.Frame(root, padding ="4 4 12 12", relief="groove", borderwidth=.5)
vrFrame.grid(column=1, row=0, rowspan = 7,sticky=(N,W,E,S))

bdFileName = StringVar()
bdRowStart = StringVar()
bdCNCol = StringVar()
bdCNumCol = StringVar()
bdTBCol = StringVar()
bdBRCol = StringVar()
bdLCCol = StringVar()

vrFileName = StringVar()
vrRowStart = StringVar()
vrCNCol = StringVar()
vrCNumCol = StringVar()
vrMSCol = StringVar()

ttk.Label(bdFrame, text="X Variables").grid(column=0, row=0, columnspan=2, padx=2)
ttk.Label(vrFrame, text="Y Report Data").grid(column=0, row=0, columnspan=2, padx=2)

bdFileNameEntry = Text(bdFrame, background = "LightSteelBlue", width=16, height=1, wrap="word")
bdFileNameEntry.tag_configure("tag-center",justify='center')
bdFileNameEntry.insert(INSERT, "bd.xlsx", "tag-center")
bdFileNameEntry.bind("<Tab>", focus_next_window)
bdFileNameEntry.bind("<Return>", focus_next_window)
bdFileNameEntry.bind("<Shift-Tab>", focus_previous_window)
bdFileNameEntry.grid(column=1, row = 1, sticky=(N,S,E,W))
ttk.Label(bdFrame, text="File Name").grid(column=0, row=1, sticky=(N,S,W,E))

bdRowStartEntry = Text(bdFrame, background = "LightSteelBlue", width=16, height=1, wrap="word")
bdRowStartEntry.tag_configure("tag-center",justify='center')
bdRowStartEntry.insert(INSERT, "6", "tag-center")
bdRowStartEntry.bind("<Tab>", focus_next_window)
bdRowStartEntry.bind("<Return>", focus_next_window)
bdRowStartEntry.bind("<Shift-Tab>", focus_previous_window)
bdRowStartEntry.grid(column=1, row = 2, sticky=(N,S,E,W))
ttk.Label(bdFrame, text="Row Start").grid(column=0, row=2, sticky=(N,S,W,E))

bdCNColEntry = Text(bdFrame, background = "LightSteelBlue", width=16, height=1, wrap="word")
bdCNColEntry.tag_configure("tag-center",justify='center')
bdCNColEntry.insert(INSERT, "B", "tag-center")
bdCNColEntry.bind("<Tab>", focus_next_window)
bdCNColEntry.bind("<Return>", focus_next_window)
bdCNColEntry.bind("<Shift-Tab>", focus_previous_window)
bdCNColEntry.grid(column=1,row=3, sticky=(N,S,E,W))
ttk.Label(bdFrame, text="Customer Name Column").grid(column=0, row=3, sticky=(N,S,W,E))

bdCNumColEntry = Text(bdFrame, background = "LightSteelBlue", width=16, height=1, wrap="word")
bdCNumColEntry.tag_configure("tag-center",justify='center')
bdCNumColEntry.insert(INSERT, "C", "tag-center")
bdCNumColEntry.bind("<Tab>", focus_next_window)
bdCNumColEntry.bind("<Return>", focus_next_window)
bdCNumColEntry.bind("<Shift-Tab>", focus_previous_window)
bdCNumColEntry.grid(column=1,row=4, sticky=(N,S,E,W))
ttk.Label(bdFrame, text="Customer Code Column").grid(column=0, row=4, sticky=(N,S,W,E))

bdTBColEntry = Text(bdFrame, background = "LightSteelBlue", width=16, height=1, wrap="word")
bdTBColEntry.tag_configure("tag-center",justify='center')
bdTBColEntry.insert(INSERT, "G", "tag-center")
bdTBColEntry.bind("<Tab>", focus_next_window)
bdTBColEntry.bind("<Return>", focus_next_window)
bdTBColEntry.bind("<Shift-Tab>", focus_previous_window)
bdTBColEntry.grid(column=1,row=5, sticky=(N,S,E,W))
ttk.Label(bdFrame, text="TotOwnBrNetRev Column").grid(column=0, row=5, sticky=(N,S,W,E))

bdBRColEntry = Text(bdFrame, background = "LightSteelBlue", width=16, height=1, wrap="word")
bdBRColEntry.tag_configure("tag-center",justify='center')
bdBRColEntry.insert(INSERT, "J", "tag-center")
bdBRColEntry.bind("<Tab>", focus_next_window)
bdBRColEntry.bind("<Return>", focus_next_window)
bdBRColEntry.bind("<Shift-Tab>", focus_previous_window)
bdBRColEntry.grid(column=1,row=6, sticky=(N,S,E,W))
ttk.Label(bdFrame, text="BrNetRevPcnt Column").grid(column=0, row=6, sticky=(N,S,W,E))

bdLCColEntry = Text(bdFrame, background = "LightSteelBlue", width=16, height=1, wrap="word")
bdLCColEntry.tag_configure("tag-center",justify='center')
bdLCColEntry.insert(INSERT, "D", "tag-center")
bdLCColEntry.bind("<Tab>", focus_next_window)
bdLCColEntry.bind("<Return>", focus_next_window)
bdLCColEntry.bind("<Shift-Tab>", focus_previous_window)
bdLCColEntry.grid(column=1,row=7, sticky=(N,S,E,W))
ttk.Label(bdFrame, text="LoadCount Column").grid(column=0, row=7, sticky=(N,S,W,E))

vrFileNameEntry = Text(vrFrame, background = "LightSteelBlue", width=16, height=1, wrap="word")
vrFileNameEntry.tag_configure("tag-center",justify='center')
vrFileNameEntry.insert(INSERT, "vr.xlsx", "tag-center")
vrFileNameEntry.bind("<Tab>", focus_next_window)
vrFileNameEntry.bind("<Return>", focus_next_window)
vrFileNameEntry.bind("<Shift-Tab>", focus_previous_window)
vrFileNameEntry.grid(column=1, row = 1, sticky=(N,S,E,W))
ttk.Label(vrFrame, text="File Name").grid(column=0, row=1, sticky=(N,S,W,E))

vrRowStartEntry = Text(vrFrame, background = "LightSteelBlue", width=16, height = 1, wrap="word")
vrRowStartEntry.tag_configure("tag-center",justify='center')
vrRowStartEntry.insert(INSERT, "2", "tag-center")
vrRowStartEntry.bind("<Tab>", focus_next_window)
vrRowStartEntry.bind("<Return>", focus_next_window)
vrRowStartEntry.bind("<Shift-Tab>", focus_previous_window)
vrRowStartEntry.grid(column=1, row = 2, sticky=(N,S,E,W))
ttk.Label(vrFrame, text="Row Start").grid(column=0, row=2, sticky=(N,S,W,E))

vrCNColEntry = Text(vrFrame, background = "LightSteelBlue", width=16, height=1, wrap="word")
vrCNColEntry.tag_configure("tag-center",justify='center')
vrCNColEntry.insert(INSERT, "A", "tag-center")
vrCNColEntry.bind("<Tab>", focus_next_window)
vrCNColEntry.bind("<Return>", focus_next_window)
vrCNColEntry.bind("<Shift-Tab>", focus_previous_window)
vrCNColEntry.grid(column=1, row = 3, sticky=(N,S,E,W))
ttk.Label(vrFrame, text="Customer Name Column").grid(column=0, row=3, sticky=(N,S,W,E))

vrCNumColEntry = Text(vrFrame, background = "LightSteelBlue", width=16, height=1, wrap="word")
vrCNumColEntry.tag_configure("tag-center",justify='center')
vrCNumColEntry.insert(INSERT, "A", "tag-center")
vrCNumColEntry.bind("<Tab>", focus_next_window)
vrCNumColEntry.bind("<Return>", focus_next_window)
vrCNumColEntry.bind("<Shift-Tab>", focus_previous_window)
vrCNumColEntry.grid(column=1, row = 4, sticky=(N,S,E,W))
ttk.Label(vrFrame, text="Customer Number Column").grid(column=0, row=4, sticky=(N,S,W,E))

vrMSColEntry = Text(vrFrame, background = "LightSteelBlue", width=16, height=1, wrap="word")
vrMSColEntry.tag_configure("tag-center",justify='center')
vrMSColEntry.insert(INSERT, "A", "tag-center")
vrMSColEntry.bind("<Tab>", focus_next_window)
vrMSColEntry.bind("<Return>", focus_next_window)
vrMSColEntry.bind("<Shift-Tab>", focus_previous_window)
vrMSColEntry.grid(column=1, row = 5, sticky=(N,S,E,W))
ttk.Label(vrFrame, text="Market Share Column").grid(column=0, row=5, sticky=(N,S,W,E))

vrMSColEntry = Text(vrFrame, background = "LightSteelBlue", width=16, height=1, wrap="word")
vrMSColEntry.tag_configure("tag-center",justify='center')
vrMSColEntry.insert(INSERT, "A", "tag-center")
vrMSColEntry.bind("<Tab>", focus_next_window)
vrMSColEntry.bind("<Return>", focus_next_window)
vrMSColEntry.bind("<Shift-Tab>", focus_previous_window)
vrMSColEntry.grid(column=1, row = 6, sticky=(N,S,E,W))
ttk.Label(vrFrame, text="Market Share Column").grid(column=0, row=6, sticky=(N,S,W,E))


ttk.Button(bdFrame, text="Calculate", command=saveValues).grid(column=0, row=8, columnspan=2, sticky=(N,S,E,W))
ttk.Button(vrFrame, text="Cancel", command=saveValues).grid(column=0, row=8, columnspan=2, sticky=(N,S,E,W))

for y in range (0,7):
    root.rowconfigure(y, weight = 1)
    bdFrame.rowconfigure(y, weight = 1)
    vrFrame.rowconfigure(y, weight = 1)
bdFrame.rowconfigure(7, weight = 1)
for x in range (0, 2):
    root.columnconfigure(x, weight = 2)
    bdFrame.columnconfigure(x, weight = 1)
    vrFrame.columnconfigure(x, weight = 1)

for child in bdFrame.winfo_children(): child.grid_configure(padx=5, pady=5)
for child in vrFrame.winfo_children(): child.grid_configure(padx=5, pady=7)

bdFileNameEntry.focus()

root.mainloop()

1 个答案:

答案 0 :(得分:0)

假设您无法使用具有内置Entry属性的justify窗口小部件,或许最简单的解决方案是在任何重新标记数据的密钥版本上添加绑定在小部件中:

def retag(event):
    event.widget.tag_add("tag-center", "1.0", "end")
...
bdFileNameEntry.bind("<Any-KeyRelease>", retag)