简体   繁体   中英

how to justify a text in a label in python?

I'm a begginer to python. I want to justify text in a label in python. Here is my code.But it isn't working. So please tell me how to justify text ina label in python. I putted "anchor='e'" into the label code either.But it doesn't work.

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

You can add the justify parameter to the label. The default value is centre if it is not included

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

The below link goes into more depth on the label widget https://www.tutorialspoint.com/python/tk_label.htm

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM