0

I am trying to build a python application and got a little stuck with the placement of the tkinter Frames. I have three frames; one for news, one for calendar and one for Quotes. The placement of news and calendar Frames is okay but i cant get the Quotes Frame to be placed under them, justified to the center. Please help me out with this placement because i cant figure it out myself. Any help is appreciated.

Posted below is the code: -

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' ,bg='black', 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',bg='black', 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, font='times 12',bg='black', 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, font='times 12',bg='black', 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", bg='black', 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", bg='black', fg='white')
        eventTitle.pack()
        eventline = '______________________'
        event1 = ''
        self.event1_lb = Label(self, font='courier 12',bg='black', fg='white')
        self.event1_lb.pack()
        self.eventline1_lb = Label(self, text = eventline, bg='black',fg='white')
        self.eventline1_lb.pack()
        event234 = ''
        self.event2_lb = Label(self, font='courier 12', bg='black', fg='white')
        self.event2_lb.pack()
        self.eventline2_lb = Label(self, text = eventline, bg='black',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",bg='black', 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, text="To do List", font="Courier 25", bg='black', fg='white')
        eventTitle.pack()
        event1_lb = Label(self, text= event123, font='courier 12',bg='black', fg='white')
        event1_lb.pack()
        #event12_lb = Label(self, text=event12, font='courier 12', bg='black',fg='white')
        #event12_lb.pack()
        eventline1_lb = Label(self, text = eventline, bg='black',fg='white')
        eventline1_lb.pack()
        event2_lb = Label(self, text=event2, font='courier 12', bg='black', fg='white')
        event2_lb.pack()        
        event22_lb = Label(self, text=event22, font='courier 12',bg='black', fg='white')
        event22_lb.pack()
        eventline2_lb = Label(self, text = eventline, bg='black',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"),bg='black',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, fill=BOTH, expand=YES)
        self.bFrame = Frame(self.tk, bg='black')
        self.bFrame.pack(side=BOTTOM, fill=BOTH, 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, anchor=NE, padx=50, pady=60
        self.weather = Weather(self.tFrame)
        self.weather.pack(side=LEFT, anchor=NW, padx=50, pady=25) #side=LEFT, anchor=NW, padx=50, pady=25
        self.news = News(self.bFrame)
        self.news.pack(side=LEFT, anchor=W, padx=25,pady=50)#side=LEFT, anchor=W, padx=25,pady=50 #new news
        self.calendar = Calendar(self.bFrame)
        self.calendar.pack(side=RIGHT,anchor=E,padx=110,pady=50)#side=RIGHT,anchor=E,padx=110,pady=50 #new calendar
        self.quotes = Quotes(self.bFrame)
        self.quotes.pack(side=RIGHT, anchor=E, padx=25, 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
abdulbasit
  • 63
  • 2
  • 9
  • If all you're asking about is the placement of frames, you can remove most of the code you've posted. Start with three empty frames and try to arrange them how you want, _then_ worry about what goes inside them. – Bryan Oakley Nov 10 '19 at 15:34
  • Any hint on how to get my third widget to be placed below my news and calendar frame? – abdulbasit Nov 10 '19 at 16:43
  • Sorry, there's just too much code to wade through. Please try to reduce the code down to a [mcve]. You're asking about three frames but I count five frames in your code, plus I think there's a couple missing frames. – Bryan Oakley Nov 10 '19 at 17:01
  • yep i removed the code of the other frames so theres only relevant code. Okay i will look for it myself – abdulbasit Nov 10 '19 at 17:57
  • 1
    I recommend you read this answer to get a basic understanding of how `pack` works: https://stackoverflow.com/a/57396569/7432 – Bryan Oakley Nov 10 '19 at 19:35

0 Answers0