Search code examples
python-3.xtkintertextpositionmove

Positioning out put text from API


the issue is trying to get the text from my output to a position to the left or how i like it.

I've tried using: sample.place(samplewindow, anchor = 'nw')

I have also tried doing it directly from the return command "Im new to python"

This is the code that im using to "print out" my response from the website.


def formate_response(weather):

    try:

        name = (weather['city']['name'])

        temp = (weather['list'][0]['main']['temp'])

        description = (weather['list'][1]['weather'][0]['description'])

        final_str = "City: " + str(name) + "\n Temperature: (°F)" + str(temp) + " \nSky: " + str(description)

        return final_str

    except:

           final_str = "Sorry, the city you have entered doesnt exist."

           return final_str

this is also the code that i use for the label

lable2 = Label(frame2, bg = 'white',)

lable2.place( relheight = 1, relwidth = 1, anchor = 'nw',)

I know some stuff isn't spelled correctly in my code but it works. I expect to move the string to the top left of the label but it doesn't work.


Solution

  • I'm not sure I have understood your question correctly as your code is not runnable, but here's an example of putting a text in the desired place.

    from tkinter import  *
    
    root = Tk()
    
    # Fixed size frame to put the label widget in
    frame2 = Frame(root, width=200, height=100)
    frame2.pack(expand=True, fill='both', pady=10, padx=10)
    
    # Putting text in upper left with anchor
    lable2 = Label(frame2, text='Test text.', bg='white', anchor='nw')
    lable2.place(relheight=1, relwidth=1)
    
    root.mainloop()
    

    The anchor option is available to both place() geometry manager and Label() widget which may confuse things.

    In general I would recommend against using the place() geometry manager, at least if I'm planning to have more than a few widgets. The pack() and grid() geometry managers are better suited for complex layouts.