0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

I made a digital clock with changeable font and theme by PySimpleGUI.

Last updated at Posted at 2022-07-28

!caution!:This is my first post.

why did I made it?

There are a lot of websites about making Python program.

Especialy,I was looking for how to make program by PySimpleGUI
because I wanted to make program with stylish GUI.
And then I met "digital clock".


I made programs as websites said first, but it was getting boring.
I thought to make "the digital clock with changeable font and theme".

My program

import PySimpleGUI as sg
from datetime import datetime
import tkinter.font

#getting current time and return it
def now():
    ntime=datetime.now()
    nt=ntime.strftime('%H:%M:%S')
    return nt

#make ordinary window
def make_window(theme,cfont):
    if theme:
        sg.theme(theme)
  #ordinary window's layout
  #current time、theme change button、font change button、close button
    layout=[[sg.Text('',key='-time-',font=(cfont,40),justification='center')],
        [sg.Button('change theme',key='-theme-',size=(10,3))],
        [sg.Button('change clock font',key='-font-',size=(10,3))],
        [sg.Button('close',key='-close-',size=(10,3))]]

    return sg.Window('digital clock',layout,size=(300,250))    
    

def main():
    #preset font and theme
    cfont='Times New Roman'
    theme='Black'
    wnd=make_window(theme,cfont)

    while True:
    #if you set timeout, you can operate GUI by spend time
        event,values=wnd.read(timeout=10,timeout_key='-timeout-')
        #getting current time per 10ms
        if event == '-timeout-':
            wnd['-time-'].update(now())
    
        if event in (sg.WIN_CLOSED,'-close-'):
            break

        #when click theme change button, open the window with pulldown menu.
        if event == '-theme-':
            #layout and window create
            event,values = sg.Window('Theme Browser',
            [[sg.Text('theme browsing')],
            [sg.Text('click theme color')],
            [sg.Combo(values=sg.theme_list(),size=(20,12),key='-LIST-',readonly=True)],
            [sg.OK(),sg.Cancel()]]).read(close=True)

            #select and click 'OK', change theme value 
            #and pass over  make_window function it
            if event == 'OK':
                theme=values['-LIST-']
                wnd.close()
                wnd= make_window(theme,cfont) 
        #when click font change button, open the window its looks like theme window
        if event == '-font-':
            #layout and window create
            event,values = sg.Window('Font Browser',
            [[sg.Text('Font browsing')],
            [sg.Text('click font')],
            [sg.Combo(values=tkinter.font.families(),size=(20,12),key='-FONT-',readonly=True)],
            # or
            # [sg.Combo(values=sg.Text.fonts_installed_list(),size=(20,12),key='-FONT-',readonly=True)],
            [sg.OK(),sg.Cancel()]]).read(close=True)

            #change font value and pass over make_window function it
            if event == 'OK':
                cfont=values['-FONT-']
                wnd.close()
                wnd= make_window(theme,cfont)
                
            
    wnd.close()

if __name__ == '__main__':
    main()

8/4/2022 updated
PySimpleGUI official gave me TIP on Twitter.
sg.Text.fonts_installed_list() show us fonts installed.

Extra

PySimpleGUI can't show font list, so I use tkinter.font.famlies()
to insert values to the pulldown menu.

Reference websites

changing theme with running the program

how to create digital clock program with PySimpleGUI(Japanese site)

how to confilm font list(Japanese site)

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?