我是python的入门者。 我想证明python标签中的文本合理。 这是我的代码,但是没有用。因此,请告诉我如何在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()
答案 0 :(得分:3)
您可以在标签上添加 justify 参数。如果不包含默认值,则为center
label = Label(canvas,anchor='w', font=("Courier", 20), compound=RIGHT,bg='white',bd=4, justify="left")
下面的链接在标签小部件上更深入 https://www.tutorialspoint.com/python/tk_label.htm