从API放置放置文本

问题描述 投票:0回答:1

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

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

我也尝试过直接通过返回命令“ Im new to python”来做到这一点

这是我用来从网站“打印”我的回复的代码。


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',)

我知道某些东西在我的代码中拼写不正确,但是可以。我希望将字符串移动到标签的左上方但这不起作用。

python-3.x tkinter text position move
1个回答
0
投票

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

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

[anchor]选项可用于place()几何图形管理器和Label()小部件,这可能会使事情变得混乱。

[通常,我建议不要使用place()几何图形管理器,至少在我打算使用多个小部件的情况下。 pack()grid()几何图形管理器更适合复杂的布局。

© www.soinside.com 2019 - 2024. All rights reserved.