tkinter框架python的放置

我正在尝试构建python应用程序,并且在tkinter Frames的放置上有些卡住。我有三帧;一种用于新闻,一种用于日历,一种用于报价。新闻和日历框架的放置还可以,但是我无法将引号框架放置在它们的下方,并居中对齐。请帮助我解决这个问题,因为我自己也无法解决。任何帮助表示赞赏。

下面发布的是代码:-

from tkinter import *
import time
import datetime
from PIL import Image,ImageTk
import requests
import calendar
from apiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
import pickle
import googlefonts_installer
from bs4 import BeautifulSoup

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='times 20',bg='black',fg='white')
        self.title_lb.pack(side=TOP,anchor=W,pady=15)
        #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,bg='black')
        self.img.pack(anchor=W)
        news = ''
        self.headline1_lb = Label(self,font = 'times 12',fg='white')
        self.headline1_lb.pack(anchor=W)
        self.img2 = Label(self,bg='black')
        self.img2.pack(anchor=W)
        news2 = ''
        self.headline2_lb = Label(self,font='times 12',fg='white')
        self.headline2_lb.pack(anchor=W)
        self.img3 = Label(self,bg='black')
        self.img3.pack(anchor=W)
        news3 = ''
        self.headline3_lb = Label(self,fg='white')
        self.headline3_lb.pack(anchor=W)
        self.img4 = Label(self,bg='black')
        self.img4.pack(anchor=W)
        news4 = ''
        self.headline4_lb = Label(self,fg='white')
        self.headline4_lb.pack(anchor=W)
        self.img5 = Label(self,bg='black')
        #self.img5.pack(anchor=W)
        news5 = ''
        self.headline5_lb = Label(self,font="times 12",fg='white')
        #self.headline5_lb.pack(anchor=W)
        self.show_news()

    def show_news(self):
        url = " https://newsapi.org/v1/articles?source=bbc-news&sortBy=top&apiKey=caa7f97ce8f2400a9785cbe704afc345"
        json = requests.get(url).json()    
        im = Image.open('Newspaper_reduced.png')
        self.pho = ImageTk.PhotoImage(im)
        self.img.configure(image=self.pho)
        self.news1 = json['articles'][0]['title']
        self.headline1_lb.configure(text=self.news1)
        self.img2.configure(image=self.pho)
        self.news2 = json['articles'][1]['title'] 
        self.headline2_lb.configure(text=self.news2)
        self.img3.configure(image=self.pho)
        self.news3 = json['articles'][2]['title']  
        self.headline3_lb.configure(text=self.news3)
        self.img4.configure(image=self.pho)
        self.news4 = json['articles'][3]['title']  
        self.headline4_lb.configure(text=self.news4)
        self.img5.configure(image=self.pho)
        self.news5 = json['articles'][4]['title']
        self.headline5_lb.configure(text=self.news5)
        self.after(600000,self.show_news)

class Calendar(Frame):                 
    def __init__(self,parent):
        super(Calendar,self).__init__(bg='black')
        '''eventTitle = Label(self,text="To do List",font="Courier 25",fg='white')
        eventTitle.pack()
        eventline = '______________________'
        event1 = ''
        self.event1_lb = Label(self,font='courier 12',fg='white')
        self.event1_lb.pack()
        self.eventline1_lb = Label(self,text = eventline,fg='white')
        self.eventline1_lb.pack()
        event234 = ''
        self.event2_lb = Label(self,fg='white')
        self.event2_lb.pack()
        self.eventline2_lb = Label(self,fg='white')
        self.eventline2_lb.pack()'''
        self.calendars()
        #self.reminders()

    def calendars(self):    
        cal = calendar.month(2019,10)
        self.calendarlb = Label(self,text=cal,font="Courier 12",justify="left",fg='white')
        self.calendarlb.pack(side=TOP,anchor=N,fill=BOTH,expand=YES)

    def reminders(self):
        scopes = ['https://www.googleapis.com/auth/calendar']
        #flow = InstalledAppFlow.from_client_secrets_file("client_secret.json",scopes=scopes)
        #credentials = flow.run_console()
        #pickle.dump(credentials,open("token.pkl","wb"))
        credentials = pickle.load(open("token.pkl","rb"))
        service = build("calendar","v3",credentials=credentials)
        result = service.calendarList().list().execute()
        calendar_id = result['items'][1]['id']
        now = datetime.datetime.utcnow()
        today = now.isoformat()+'Z'
        tomorrow = (now+datetime.timedelta(days=1)).isoformat()+'Z'
        outcomes = service.events().list(calendarId=calendar_id,timeMin=today,timeMax=tomorrow,singleEvents=True,orderBy='startTime').execute()
        self.eventa = outcomes['items'][0]['summary']
        self.eventb = outcomes['items'][0]['end']['dateTime']
        self.event1 = str(self.eventa + '\n' + self.eventb)
        self.event1_lb.configure(text=self.event1)
        self.event2 = outcomes['items'][1]['summary']
        self.event23 = outcomes['items'][1]['end']['dateTime']
        self.event234 = str(self.event2 + '\n' + self.event23)
        self.event2_lb.configure(text=self.event234)
        '''eventline = '______________________'
        eventTitle = Label(self,fg='white')
        eventTitle.pack()
        event1_lb = Label(self,text= event123,fg='white')
        event1_lb.pack()
        #event12_lb = Label(self,text=event12,fg='white')
        #event12_lb.pack()
        eventline1_lb = Label(self,fg='white')
        eventline1_lb.pack()
        event2_lb = Label(self,text=event2,fg='white')
        event2_lb.pack()        
        event22_lb = Label(self,text=event22,fg='white')
        event22_lb.pack()
        eventline2_lb = Label(self,fg='white')
        eventline2_lb.pack()'''
        self.after(1000,self.reminders)

class Quotes(Frame):
    def __init__(self,parent):
        super(Quotes,self).__init__(bg='black')
        req = requests.get('https://www.brainyquote.com/quote_of_the_day')
        soup = BeautifulSoup(req.text,'lxml')
        imageQuote = soup.find('img')
        #print(imageQuote['alt'])
        text_lb = Label(self,text=imageQuote['alt'],font=("Courier 12"),fg='white')
        text_lb.pack(side=RIGHT)


class FullscreenWindow:
    def __init__(self):
        self.tk = Tk()
        self.tk.configure(bg='black')
        self.tk.title('smartmirror')
        self.tFrame = Frame(self.tk,bg='black')
        self.tFrame.pack(side=TOP,expand=YES)
        self.bFrame = Frame(self.tk,bg='black')
        self.bFrame.pack(side=BOTTOM,expand=YES)
        self.state = False
        self.tk.bind("<Return>",self.toggle_fullscreen)
        self.tk.bind("<Escape>",self.end_fullscreen)
        #self.logo = Logo(self.topFrame)
        #self.logo.pack(side=TOP,anchor=CENTER)
        self.clock = Clock(self.tFrame)
        self.clock.pack(side=RIGHT,anchor=NE,padx=50,pady=60) #side=RIGHT,pady=60
        self.weather = Weather(self.tFrame)
        self.weather.pack(side=LEFT,anchor=NW,pady=25) #side=LEFT,pady=25
        self.news = News(self.bFrame)
        self.news.pack(side=LEFT,padx=25,pady=50)#side=LEFT,pady=50 #new news
        self.calendar = Calendar(self.bFrame)
        self.calendar.pack(side=RIGHT,anchor=E,padx=110,pady=50)#side=RIGHT,pady=50 #new calendar
        self.quotes = Quotes(self.bFrame)
        self.quotes.pack(side=RIGHT,pady=40)

    def toggle_fullscreen(self,event=None):
        self.state = not self.state  # Just toggling the boolean
        self.tk.attributes("-fullscreen",self.state)
        return "break"

    def end_fullscreen(self,event=None):
        self.state = False
        self.tk.attributes("-fullscreen",False)
        return "break"    

if __name__ == '__main__':
    w = FullscreenWindow()
    w.tk.mainloop
wflwfl 回答:tkinter框架python的放置

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/3129085.html

大家都在问