Search code examples
pythonuser-interfacetkinterlabelalignment

How can I align the label to the top right at the same time in packs() method of labeling widgets in Python GUI?


I tried to use that code block to align a label to the top right or bottom left-like-sides, but packs() method doesn't allow two arguments...

See my code:

import tkinter
class guigui:
    def __init__(self):
        self.main=tkinter.Tk()
        self.label11=tkinter.Label(self.main, text="label text 1")
        self.label2=tkinter.Label(self.main, text="label text 2")
        self.label1.pack(side="right")
        self.label2.pack(side="bottom")
        tkinter.mainloop()
if __name__=='__main__':
    agui=guigui()

I wanted to align text 2 to bottom so I tried to do like that:

self.label2.pack(side="right")
self.label2.pack(side="bottom")

But it didn't work. The second line of that block is executed but the first one is always ignored, and everywhere is written in thepack() method allows only the left and right bottom top sides, okay but what am I suppose to do if I want to align label text the cross areas?


Solution

  • Typo error in line 5.

    Change this:

    self.label11
    

    to:

    self.label1
    

    Change this:

    self.label2.pack(side="right")
    self.label2.pack(side="bottom")
    

    to:

    self.label1.pack(side=tkinter.TOP)
    self.label2.pack(side=tkinter.TOP)
    

    Output:

    enter image description here