簡體   English   中英

在新行打印 tkinter label

[英]Printing tkinter label in new line

首先,很抱歉這個模糊的標題。 我正在制作一個顯示時間、獲取天氣和新聞的 python 應用程序。 現在,當我通過 tkinter label 打印新聞時,它會在單獨的一行但在中心打印新聞的標題。 如果我嘗試指定 .pack(side=LEFT) 幾何圖形,它會向左移動,但所有標題都以字符串而不是換行符打印。 我曾嘗試通過 '\n' 添加新行,甚至回車 '\n' 但徒勞無功。 請幫我解決這個問題。 附上下面的代碼。

Ps 我無法獲得使用 For 循環的消息,所以我手動打印了 arrays。

from tkinter import *
import datetime
from PIL import Image, ImageTk
import requests



class Clock(Frame):
    def __init__(self, parent):
        Frame.__init__(self,parent, bg='black')
        self.now = datetime.datetime.today()
        self.time = str(self.now.hour) + ":" + str(self.now.minute)
        self.timelb = Label(self, text=self.time, font=("Helvetica 50"), bg='black', fg='white')
        self.timelb.pack(anchor=NE,padx=60,pady=0)
        self.date = str(self.now.day) + '.' + str(self.now.month) + '.' + str(self.now.year)
        self.day = self.now.strftime('%A')
        self.daylb = Label(self, text=self.day, font="Helvetica 20", bg='black', fg='white')
        self.daylb.pack(anchor=NE,padx=60)
        self.datelb = Label(self, text=self.date, font="Helvetica 25", bg = 'black', fg='white')
        self.datelb.pack(anchor=NE, padx=60)



class Weather(Frame):
    def __init__(self, parent):
        Frame.__init__(self,parent,bg='black')
        url = 'http://api.openweathermap.org/data/2.5/weather?appid=c73d9cdb31fd6a386bee66158b116cd0&q=Karachi&units=metric'
        json = requests.get(url).json()
        temperature = json['main']['temp']
        description = json['weather'][0]['description']
        icon_id = json['weather'][0]['icon']
        city = 'Karachi'
        icon_url = ('http://openweathermap.org/img/wn/{icon}@2x.png'.format(icon=icon_id))
        self.im = Image.open(requests.get(icon_url, stream=True).raw)
        self.ph = ImageTk.PhotoImage(self.im)
        degree = u'\N{DEGREE SIGN}' + 'C'
        self.pic_label = Label(self,image=self.ph,bg='black')
        self.pic_label.pack()
        self.lab= Label(self,text=(str(temperature) + degree),font=("Helvetica 40"), bg='black', fg='white')
        self.lab.pack()
        self.description_label=Label(self, text=description, font='Helvetica 20',bg='black', fg='white')
        self.description_label.pack()
        self.city_label=Label(self, text=city, font = 'Helvetica 10', bg='black', fg='white')
        self.city_label.pack()

class News(Frame):
    def __init__(self, parent):
        super(News, self).__init__(bg='black')  
        url = " https://newsapi.org/v1/articles?source=bbc-news&sortBy=top&apiKey=caa7f97ce8f2400a9785cbe704afc345"
        json = requests.get(url).json()
        self.title = 'Headlines'
        self.title_lb = Label(self, text=self.title, font='Helvetica 25',bg='black', fg='white')
        self.title_lb.pack(side=TOP, anchor=N)
        im = Image.open('Newspaper_reduced.png')
        self.pho = ImageTk.PhotoImage(im)
        news1 = json['articles'][0]['title'] 
        news2 = json['articles'][1]['title'] 
        news3 = json['articles'][2]['title']  
        news4 = json['articles'][3]['title']  
        news5 = json['articles'][4]['title']
        self.img = Label(self,image=self.pho,bg='black')
        self.img.pack(side=LEFT)
        self.headline1_lb = Label(self, text=news1, font = 'Helvetica 15' ,bg='black', fg='white')
        self.headline1_lb.pack(side=LEFT)
        self.img2 = Label(self,image=self.pho,bg='black')
        self.img2.pack(side=LEFT)
        self.headline2_lb = Label(self, text = news2, font='Helvetica 15',bg='black', fg='white')
        self.headline2_lb.pack(side=LEFT)
        self.img3 = Label(self,image=self.pho,bg='black')
        self.img3.pack(side=LEFT)
        self.headlines3_lb = Label(self, text=news3, font='Helvetica 15',bg='black', fg='white')
        self.headlines3_lb.pack(side=LEFT)
        self.img4 = Label(self,image=self.pho,bg='black')
        self.img4.pack(side=LEFT)
        self.headlines4_lb = Label(self, text=news4, font='Helvetica 15',bg='black', fg='white')
        self.headlines4_lb.pack(side=LEFT)
        self.img5 = Label(self,image=self.pho,bg='black')
        self.img5.pack(side=LEFT)
        self.headlines5_lb = Label(self, text=news5, font='Helvetica 15',bg='black', fg='white')
        self.headlines5_lb.pack(side=LEFT)




class Fullscreen:
    def __init__(self):
        self.tk = Tk()
        self.tk.configure(bg='black')
        self.tk.title('smartmirror')
        self.topFrame = Frame(self.tk , bg='black')
        self.topFrame.pack(side=TOP, fill=BOTH, expand=YES)
        self.bottomFrame = Frame(self.tk, bg='black')
        self.bottomFrame.pack(side=BOTTOM, fill=BOTH, expand=YES)
        self.clock = Clock(self.topFrame)
        self.clock.pack(side=RIGHT, anchor=NE, padx=50, pady=60)
        self.weather = Weather(self.topFrame)
        self.weather.pack(side=LEFT, anchor=NW, padx=50, pady=70)
        self.news = News(self.bottomFrame)
        self.news.pack(side=BOTTOM, anchor=S)

if __name__ == '__main__':
    w = Fullscreen()
    w.tk.mainloop

首先:我在代碼中刪除了圖像,因為我沒有它們,我想浪費時間搜索可以用作替換的圖像。


如果您為標簽設置不同的顏色,那么您會看到不同的寬度 - 它們使用文本的寬度。

在此處輸入圖像描述

如果你使用

self.headline1_lb.pack(fill='x') 

然后他們將使用相同的寬度,但文本仍然在中心。

在此處輸入圖像描述

如果你使用

Label(..., anchor='w')

然后它將文本移動到左側。

在此處輸入圖像描述

如果您要輸入多行 Label 文本,那么您可能還需要

Label(..., anchor='w', justify='left')

如果你想使用 window 的全寬作為文本,那么你必須使用fill='x'

self.news.pack(side=BOTTOM, anchor='s', fill='x')

在此處輸入圖像描述

之后,您可以再次設置black背景。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM