Как выровнять текст в метке в Python? - PullRequest
0 голосов
/ 07 октября 2019

Я новичок в питоне. Я хочу, чтобы оправдать текст в метке в Python. Вот мой код. Но он не работает. Поэтому, пожалуйста, скажите мне, как выровнять текстовую метку в питонеЯ вставил "anchor = 'e'" в код метки. Но это не работает.

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

1 Ответ

3 голосов
/ 07 октября 2019

Вы можете добавить параметр justify к метке. Значение по умолчанию - центр, если оно не включено

label = Label(canvas,anchor='w', font=("Courier", 20), compound=RIGHT,bg='white',bd=4, justify="left")

Приведенная ниже ссылка углубляется в виджет метки https://www.tutorialspoint.com/python/tk_label.htm

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...