How can I control the position of text in a label?
Thank you

Recommended Answers

All 3 Replies

You can also use the Tkinter canvas as a label...

''' tk_canvas_create_text1.py
show text at a given x, y location on a Tkinter canvas
'''

try:
    # Python2
    import Tkinter as tk
except ImportError:
    # Python3
    import tkinter as tk

root = tk.Tk()
root.title('Tkinter canvas text')

canvas = tk.Canvas(root, height=200, width=200, bg="yellow")
canvas.pack()

# select x, y cordinates for ulc start of text
x = 15
y = 20
sa = "Hello at x=%d, y+%d" % (x, y)
text_a = canvas.create_text(x, y, anchor='nw', text=sa)

x = 45
y = 80
sb = "Hello at x=%d, y+%d" % (x, y)
text_b = canvas.create_text(x, y, anchor='nw', text=sb)

root.mainloop()

Another way, but you can use it only one time:

# allows Python27 to use Python3 print options
from __future__ import print_function

def text_xy(text, x, y):
    '''print the text at location x, y'''
    xx = " " * x
    yy = '\n' * y
    print(yy, end="")
    print(xx, end="")
    print(text)

text = 'Hello'
x = 20
y = 10
text_xy(text, x, y)
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.