0

I am using Tkinter for Python right now, and I'm trying to figure out if it's possible to recenter text after a user inputs something into a Text widget.

Currently, my code outputs like this:

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

What I'd like is for the entered text to appear centered within the text field. My code is below. All of the text entries are essentially the same, so most of the code is similar. I only included it for comprehensiveness, but I'm pretty sure it just has to deal with modifying the Text(...) object.

Thank you.

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()
RWA4ARC
  • 109
  • 3
  • 14
  • 1
    I see that you're repeating a lot in your code. Try creating functions or classes for your entries instead. It will make your code more readable and reduce the amount of lines. More readable code will make more people being able to help :) Also, put your imports in your code so people can test your code for themselves. – Ted Klein Bergman Aug 02 '16 at 17:34
  • What's the reason for using a multiline text widget for single line input? – Bryan Oakley Aug 02 '16 at 17:45
  • @BryanOakley At some point, I needed it for some functionality entry couldn't handle, but I'm blanking on the reason now. I could change it back, but I remember having a good reason (weighting/spacing maybe) for changing it. – RWA4ARC Aug 02 '16 at 17:56
  • The `Entry` widget has an attribute named `justify` which can be set to `"center"`. – Bryan Oakley Aug 02 '16 at 18:00
  • Will this work though after text has been entered? Or will it fail to update like the Text object has? – RWA4ARC Aug 02 '16 at 18:01
  • 1
    I made function that creates your entries and got the code size down by 2/3 (from 163 lines to 60 lines). I does exactly the same but is just less code to read through: http://hastebin.com/axowekubun.coffee – Ted Klein Bergman Aug 02 '16 at 18:31
  • @TedKleinBergman Wow, thank you Ted. I actually tried creating some classes but I'm having a little trouble with it. – RWA4ARC Aug 02 '16 at 18:44
  • 1
    @DouglasTrippe You could make a class instead without changing much (http://hastebin.com/mecekukaqa.coffee), but since the class only contain the `__init__()` method you probably should use a function (unless you need some own methods bound to each entry object). When you see code that's similar, try to identify what's the same and what differs. What differs goes as parameters and the rest in the body of the function. – Ted Klein Bergman Aug 02 '16 at 18:56
  • @TedKleinBergman I actually made something similar (http://hastebin.com/tomurasowi.py), but originally had `Text.__init__....`. Yeah, I've worked a lot with classes before, just never in Python. I was also admittedly being a bit lazy, but thank you for the help. I think I might also force myself to use entry, because it does make more sense. – RWA4ARC Aug 02 '16 at 18:58
  • @TedKleinBergman Also, is super().__init__ the typical way of inheriting properties/methods of the parent class? I had some trouble understanding that. Originally, my bdFileNameEntry.get function failed for multiple reasons. – RWA4ARC Aug 02 '16 at 19:00
  • 1
    @DouglasTrippe Yes, I would say `super()` is the typical way. Although, in Python 2.x it's `super(MyClass, self).__init__()`. It can be a bit complicated when inheriting multiple classes but otherwise it's quite straight forward. Here's a great article explaining the powers of `super()` https://rhettinger.wordpress.com/2011/05/26/super-considered-super/ and here's a SO answer explaining it aswell http://stackoverflow.com/questions/222877/how-to-use-super-in-python – Ted Klein Bergman Aug 02 '16 at 19:06
  • @TedKleinBergman Thank you a ton for the info. I'm using Python 2.7 right now, so I was about to ask why super() was failing, but now it's obvious. Thanks again. – RWA4ARC Aug 02 '16 at 19:09

1 Answers1

0

Assuming you can't use an Entry widget which has a built-in justify attribute, perhaps the simplest solution is to add a binding on any key release which re-tags the data in the widget:

def retag(event):
    event.widget.tag_add("tag-center", "1.0", "end")
...
bdFileNameEntry.bind("<Any-KeyRelease>", retag)
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685