从 API 定位输出文本

Positioning out put text from API

问题是试图将文本从我的输出中获取到左侧位置或我喜欢的位置。

我试过使用: sample.place(samplewindow, anchor = 'nw')

我也试过直接从 return 命令 "Im new to python"

这是我用来 "print out" 我从网站回复的代码。


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

这也是我用于标签的代码

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

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

我知道我的代码中有些内容拼写不正确,但它有效。 我希望将字符串移动到标签的左上角 但它不起作用。

我不确定我是否正确理解了你的问题,因为你的代码不可运行,但这里有一个将文本放在所需位置的示例。

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()

锚点选项可用于 place() 几何管理器和 Label() 小部件,这可能会造成混淆。

总的来说,我不建议使用 place() 几何管理器,至少在我计划拥有多个小部件的情况下。 pack()grid() 几何管理器更适合复杂的布局。