How to center text in text widget in tkinter - python

I have a text widget
from tkinter import *
root = Tk()
root.wm_title("cloth")
root.geometry("500x500")
l = Text(root, width=46, bg="White",relief = "solid", font="Arial 9",height=1,bd=1)
l.configure(state='normal')
l.configure(state='disabled')
l.grid(row=1, column=1)
I want to center text
but if I select then only the text is selected, no space is selected

You need add tag_configure("tag_name", justify='center') and tag_add("tag_name", "1.0", "end")
Here is the example
from tkinter import *
root = Tk()
root.wm_title("cloth")
root.geometry("500x500")
l = Text(root, width=46, bg="White",relief = "solid", font="Arial 9",height=1,bd=1)
l.tag_configure("tag_name", justify='center')
l.insert("1.0", "How to center text in text widget in tkinter ?")
l.tag_add("tag_name", "1.0", "end")
l.grid(row=1, column=1)

I think, no way to do it without adding spaces
import tkinter as tk
root = tk.Tk()
T1 = tk.Text(root)
T1.tag_configure("center", justify='center')
T1.insert(1.0, " ")
T1.tag_add("center", "1.0", "end")
T1.pack()
root.mainloop()

Related

Justify text to the top of label in tkinter

in my code every time I press enter while typing on a entry widget the text on the entry is printed on a label below it but the text is always in the middle of the label. is there a way to make it print on the top?
from tkinter import *
root = Tk()
root.state('zoomed')
def AddList(event):
de = dataInput.get()
d = dataList['text'] + de + "\n"
dataList.config(text=d)
dataInput.delete('0', 'end')
dataLabel = Label(root, width=4, text="Dados").grid(column=0, row=0)
dataInput = Entry(root,width=4)
dataInput.bind('<Return>', AddList)
dataInput.grid(column=0, row=1)
dataList = Label(root, text="", width=4, height=43, bg='#8c8c8c')
dataList.grid(column=0, row=2, sticky=NS)
root.mainloop()
Yes there is a way:
By setting anchor='n', you force the text to be on the top.
dataList = Label(root, text="", width=4, height=43, bg='#8c8c8c',anchor='n')

How to print the items in a tkinter window?

I have this code:
root= tk.Tk()
button1 = tk.Button(root, text='Button', command=lambda:print("Click"))
button1.grid(row=1, column=1)
Label = tk.Label(root, text="Text")
Label.grid(row=1, column=2)
root.mainloop()
What I want to get:
Button:(1, 1), Label:(1, 2)
How can I print the items and their destinations in root?
If you just want to get the information about grid, use grid_info() could get the info about the widget.To get all the widgets on root, just use root.winfo_children().
import tkinter as tk
root = tk.Tk()
button1 = tk.Button(root, text='Button', command=lambda:print("Click"))
button1.grid(row=1, column=1)
Label = tk.Label(root, text="Text")
Label.grid(row=1, column=2)
for widget in root.winfo_children():
print(f"{widget.widgetName}:({ widget.grid_info()['row']}, {widget.grid_info()['column']})")
root.mainloop()
Result:
button:(1, 1)
label:(1, 2)
Would this solve it? What you do is create a function that generates a label in root and then give it to the command argument of your button.
import tkinter as tk
root= tk.Tk()
def x():
y = tk.Label(root, text = "Click")
y.grid(row = 2, column = 1)
button1 = tk.Button(root, text='Button', command=x)
button1.grid(row=1, column=1)
Label = tk.Label(root, text="Text")
Label.grid(row = 2, column = 1)
root.mainloop()

Tkinter new window

I'm relatively new to Tkinter and I need help.
I have created a new window when a button is clicked from the parent window. The new window is the def new_Window. But I can't seem to get the information in the window as coded below:
from tkinter import *
from tkinter import ttk
#User Interface Code
root = Tk() #Creates the window
root.title("Quiz Game")
def new_window():
newWindow = Toplevel(root)
display = Label(newWindow, width=200, height=50)
message = Label(root, text="Welcome")
display.pack()
message.pack()
display2 = Label(root, width=100, height=30)
button1 = Button(root, text ="Continue", command=new_window, width=16,
bg="red")
message_label = Label(root, text="Click 'Continue' to begin.",
wraplength=250)
username = StringVar() #Stores the username in text
user_entry = Entry(root, textvariable=username) #Creates an entry for the
username
user_entry.pack()
display2.pack()
button1.pack()
message_label.pack()
root.mainloop()#Runs the main window loop
Thanks for your help.
You did not pack the hello label into the new window. A tip is also to use background colors to visualize labels when developing. Here is a functioning code for you. I only changed 2 lines, added foreground and background.
from tkinter import *
from tkinter import ttk
# User Interface Code
root = Tk() # Creates the window
root.title("Quiz Game")
def new_window():
newWindow = Toplevel(root)
display = Label(newWindow, width=200, height=50,bg='RED')
message = Label(newWindow, text="HEEEY",fg='BLACK',bg='GREEN')
message.pack()
display.pack()
display2 = Label(root, width=100, height=30)
button1 = Button(root, text ="Continue", command=new_window, width=16,
bg="red")
message_label = Label(root, text="Click 'Continue' to begin.",
wraplength=250)
username = StringVar() # Stores the username in text
user_entry = Entry(root, textvariable=username) # Creates an entry for the
username
user_entry.pack()
display2.pack()
button1.pack()
message_label.pack()
root.mainloop() # Runs the main window loop

Tkinter in fullscreen mode makes Text widget not work

When using Tkinter as a regular window, the text widget works as intended and can be typed in.
from Tkinter import *
root = Tk()
T = Text(root, height=2, width=30)
T.pack()
T.insert(END, "Just a text Widget\nin two lines\n")
mainloop()
However if we make this application fullscreen:
from Tkinter import *
root = Tk()
root.attributes("-fullscreen", True)
T = Text(root, height=2, width=30)
T.pack()
T.insert(END, "Just a text Widget\nin two lines\n")
mainloop()
And try to type in the textbox, it won't work and the keys will instead appear in the terminal. Why is this and is there a solutoin?
import tkinter as tk
from tkinter import *
root = tk.Tk()
root.overrideredirect(True)
root.overrideredirect(False)
root.attributes('-fullscreen',True)
canvas= Canvas(root)
canvas.create_text(300, 50,text="This is a window", fill="black", font=('Helvetica 15 bold'))
canvas.pack()
This seems to be working with me

How to center widgets vertically and horizontally in window with Tkinter?

I need to center 3 labels vertically within the window. The labels are centered on-top of each other, but they are fixed at the top of the window.
What do I need to do to have them sit right in the middle of the window, (vertically and horizontally)?
Here is my code:
from tkinter import *
root = Tk()
root.geometry("200x200")
root.title("Question 2")
root.configure(background="green")
Label(root, text = "RED", fg="red", bg="black").pack()
Label(root, text = "WHITE", fg="white", bg="black").pack()
Label(root, text = "BLUE", fg="blue", bg="black").pack()
root.mainloop()
I think that in this case you can simply use a Frame widget as the parent of the labels and then pack the frame by setting the expand option to True:
from tkinter import *
root = Tk()
root.geometry("200x200")
root.title("Question 2")
root.configure(background="green")
parent = Frame(root)
Label(parent, text = "RED", fg="red", bg="black").pack(fill="x")
Label(parent, text = "WHITE", fg="white", bg="black").pack(fill="x")
Label(parent, text = "BLUE", fg="blue", bg="black").pack(fill="x")
parent.pack(expand=1) # same as expand=True
root.mainloop()

Categories