简体   繁体   English

如何证明 python 中的 label 中的文本是合理的?

[英]how to justify a text in a label in python?

I'm a begginer to python.我是 python 的初学者。 I want to justify text in a label in python.我想证明 python 中的 label 中的文本是合理的。 Here is my code.But it isn't working.这是我的代码。但它不起作用。 So please tell me how to justify text ina label in python.所以请告诉我如何在 python 中证明 label 中的文本。 I putted "anchor='e'" into the label code either.But it doesn't work.我也将“anchor='e'”放入 label 代码中。但它不起作用。

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.您可以将justify参数添加到 label。 The default value is centre if it is not included如果不包含则默认值为 center

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以下链接更深入地介绍了 label 小部件https://www.tutorialspoint.com/python/tk_label.htm

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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