rishi
rishi

Reputation: 640

How to format the text input from Text Widget in Tkinter

In my tkinter program I'm collecting text from the user using Text widget, this is later printed on the screen using a label widget. Although I'm able to print it onto the screen, the text is all center aligned. Since what I'm collecting is a procedure for something it gets difficult to read, so I need it to be left aligned.


This is my Procedure method - Once the procedure is collected it is stored into a dictionary

   def Procedure(self):

        textfield = Text(gui, height=30, width=82)
        textfield.place(x="20", y="100")

        procedure_label = LabelWidget(self.screen, "Procedure", "Courier", 40)
        procedure_label.Call().place(x="220", y="20")

        button_save = Button(gui, text="Next", padx="50", pady="20", bg="lightgrey",
                             command=partial(self.CheckPage, 4, procedure=textfield))
        button_save.place(x="250", y="600")

This is how I'm printing my proceudre

    proc_text_label = ""
    for i in fullDictProc:
        proc_text_label_temp = Label(root, text=i, wraplength=900)
        proc_text_label = proc_text_label_temp
    proc_text_label.config(font=("Courier", 12))
    proc_text_label.place(x=70, y=250)

Here is a minimal reproducible code to demonstrate the problem

Run it and see the alignment of the text.

from tkinter import *
from functools import partial

gui = Tk()
gui.geometry("700x700")


def printit(textfield):
    procedure_list = [textfield.get("1.0", "end-1c")]
    textfield.place_forget()
    proc_text_label = ""
    for i in procedure_list:
        proc_text_label_temp = Label(gui, text=i, wraplength=900)
        proc_text_label = proc_text_label_temp
    proc_text_label.config(font=("Courier", 12))
    proc_text_label.place(x=70, y=250)


textfield = Text(gui, height=30, width=82)
textfield.place(x="20", y="100")


button_save = Button(gui, text="Next", padx="50", pady="20", bg="lightgrey",
                     command=partial(printit, textfield))

button_save.place(x=500, y=600)
gui.mainloop()

Upvotes: 1

Views: 804

Answers (2)

figbeam
figbeam

Reputation: 7176

I think what you are looking for might be justify:

proc_text_label.config(justify='left')

Have a look at The Tkinter Label Widget

Upvotes: 3

Bastien Harkins
Bastien Harkins

Reputation: 305

I think what you're looking for is the anchor parameter. This is how it worked with your minimal example:

from tkinter import *
from functools import partial

gui = Tk()
gui.geometry("700x700")


def printit(textfield):
    procedure_list = [textfield.get("1.0", "end-1c")]
    textfield.place_forget()
    proc_text_label = ""
    for i in procedure_list:
        proc_text_label_temp = Label(gui, text=i, wraplength=900,
                                     anchor='w',
                                     bg='blue',
                                     width=50)
        proc_text_label = proc_text_label_temp
    proc_text_label.config(font=("Courier", 12))
    proc_text_label.place(x=70, y=250)


textfield = Text(gui, height=30, width=82)
textfield.place(x="20", y="100")


button_save = Button(gui, text="Next", padx="50", pady="20", bg="lightgrey",
                     command=partial(printit, textfield))

button_save.place(x=500, y=600)
gui.mainloop()

Upvotes: 1

Related Questions