Issue
I'm trying to get a single text line in the middle of the window, as in vertically and horizontally, I have been able to get it centered horizontal, but not vertically
This is what I got so far:
import PySimpleGUI as sg
import time
# ---------------- Create Form ----------------
sg.theme('Black')
sg.set_options(element_padding=(0, 0))
layout = [[sg.Text('')],
[sg.Text(size=(40, 10), font=('Helvetica Bold', 40), justification='center', key='text', expand_x=True, expand_y=True)]]
window = sg.Window('Running Timer', layout, no_titlebar=False, element_justification='c', auto_size_buttons=False, keep_on_top=True, grab_anywhere=True).Finalize()
window.Maximize()
# ---------------- main loop ----------------
current_time = 0
paused = False
start_time = int(round(time.time() * 100))
while (True):
# --------- Read and update window --------
event, values = window.read(timeout=10)
current_time = int(round(time.time() * 100)) - start_time
# --------- Display timer in window --------
window['text'].update(' {:02d}:{:02d}.{:02d}'.format((current_time // 100) // 60,
(current_time // 100) % 60,
current_time % 100))
Solution
Using the helper functions sg.Push()
ad sg.VPush()
Push()
Placing one on each side of an element will center the element horizontally. Place one to the left and the element to the right will be right justified.
VPush()
Placing one on each side of an element will center the element vertically. Place one to the top and the element to the bottom will be bottom justified.
import time
import PySimpleGUI as sg
sg.theme('Black')
sg.set_options(font=('Helvetica Bold', 40))
layout = [
[sg.VPush()],
[sg.Push(), sg.Text(key='text'),sg.Push()],
[sg.VPush()],
]
window = sg.Window('Running Timer', layout, finalize=True)
window.maximize()
current_time = 0
start_time = int(round(time.time() * 100))
while (True):
event, values = window.read(timeout=10)
if event == sg.WIN_CLOSED:
break
elif event == sg.TIMEOUT_EVENT:
current_time = int(round(time.time() * 100)) - start_time
window['text'].update(
'{:02d}:{:02d}.{:02d}'.format(
(current_time // 100) // 60,
(current_time // 100) % 60,
current_time % 100))
window.close()
Answered By - Jason Yang
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.