首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在python中对标签中的文本进行对齐?

如何在python中对标签中的文本进行对齐?
EN

Stack Overflow用户
提问于 2019-10-07 14:21:06
回答 1查看 108关注 0票数 0

我是蟒蛇的乞丐。我想在python中对标签中的文本进行对齐。这是我的code.But,它不起作用。所以请告诉我如何在python中证明文本的标签是正确的。我将“锚=‘e’”插入到标签代码either.But中,它不起作用。

代码语言:javascript
复制
from tkinter import ttk
import tkinter as tk
from tkinter import *
from PIL import Image, ImageTk


window=tk.Tk()

im = Image.open("landscape2.png")
tkimage = ImageTk.PhotoImage(im)

tab_control = ttk.Notebook(window)

tab5 = ttk.Frame(tab_control)
tab_control.add(tab5, text='History')

tab_control.pack(expand=1, fill='both')

his_lbl = tk.Label(tab5, image=tkimage)
his_lbl.place(relwidth = 1, relheight = 1)

his_frame = tk.Frame(tab5, bg='#80c1ff',bd=5)
his_frame.place(relx = 0.3, rely = 0.1, relheight=0.1, relwidth=0.50, anchor= 'n')

button = tk.Button(his_frame, bg = 'white', command = lambda: get_weather(his_entry.get()))
button.place(relx = 0.7, relheight = 1, relwidth  = 0.3)

his_entry = tk.Entry(his_frame, font =('Courier', 18))
his_entry.place(relheight = 1, relwidth = 0.65)

canvas = Canvas(tab5, bg="white")
canvas.place(relx = 0.3, rely = 0.25, relheight = 0.6, relwidth = 0.50, anchor='n')


lst = []
y = 0

label = Label(canvas,anchor='w', font=("Courier", 20), compound=RIGHT,bg='white',bd=4)
label.place(relwidth=1,relheight=1)
canvas.create_window(0, y, window=label, anchor=NW)
y += 60

scrollbar = Scrollbar(canvas, orient=VERTICAL, command=canvas.yview)
scrollbar.place(relx=1, rely=0, relheight=1, anchor=NE)
canvas.config(yscrollcommand=scrollbar.set, scrollregion=(0, 0, 0, y))

def get_weather(history):
    file=open((history+".txt"),("r"))
    a=(file.read())
    label['text'] = a
window.mainloop()
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-10-07 14:29:02

您可以向标签中添加对齐参数。如果不包括默认值,则默认值为中心。

代码语言:javascript
复制
label = Label(canvas,anchor='w', font=("Courier", 20), compound=RIGHT,bg='white',bd=4, justify="left")

下面的链接将更深入地介绍label小部件https://www.tutorialspoint.com/python/tk_label.htm

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58271516

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档