ryotankの備考録日記

趣味の電子工作についての備考録などなど

SVGからPNG変換GUIその2


作り込む為に画面レイアウトを見やすくした

svgpng変換GUIイメージ図-ver1.0の変換前_画面レイアウト用

これに沿ってツールを作成していく



出来たのがこれ

svgpngツールver1.0キャプチャ

あとフレーム要素は画面の中央に寄せたい

#画面レイアウトに従いレイアウトを作り込む
#2022-7-19作成

#ver2.0ではFrameの位置を中央に寄せる

from contextlib import closing
import PySimpleGUI as sg

sg.theme('LightGreen3')


frame1_layout =[
    [sg.Text('回路図.svg')]
]

frame2_layout =[
    [sg.Text('pngになった回路図.png')]
]


layout = [
    [sg.T()],
    [sg.Text('変換したいSVGファイルを選択して下さい')],

    [sg.Button('ファイルを選択', font=16, key="-FILE-CHO-")],
    [sg.Frame(title='変換したいsvgのサムネ', layout=frame1_layout)],
    [sg.Text('変換処理のステータス表示', font=18)],
    [sg.ProgressBar(key="-PROCESSING-", size=(30, 30), max_value=100)],
    [sg.Text('実行後にファイルがpngに変換されます', font=20)],
    [sg.Frame(title='pngファイル',layout=frame2_layout)],
    [sg.T('')],
    [sg.Button('別名で保存', key="-BET-", button_color='red')],
    [sg.T('')]
]


def gen_bytes_count(filepath):
    
    import os

    # Total Bytes

    filesize = os.stat(filepath).st_size

    count = 0



    # NOTE: バイト数カウントのためバイナリで開いてます

    # テキストで開いた場合、文字数 != バイト数(ファイルサイズ) と一致しません

    with open(filepath, "rb") as infile:

        for line in infile:

            count += len(line)

            yield (int(100*count/filesize), count)



task = None

options = {}

timeout_options = {"timeout":100, "timeout_key":"-timeout-"}



def task_cancel():

    global task

    task = None

    options.clear()



def task_start(filepath):

    global task

    task = gen_bytes_count(filepath)

    options.update(timeout_options)




with closing(sg.Window('svgをpngに変換ツールver1.0', layout)) as window:

    while True:
    
        event, values = window.read(**options)

        # print(event)

        if not event:

            break

        elif event == "-STOP-":

            task_cancel()

        elif event == "-START-":

            task_start(values["-INPUT-FILE-"])

        elif event == "-timeout-":

            if not task: # 処理するファイルが無い時

                continue

            info = next(task, None)

            if not info: # ファイル処理が終わった時

                task_cancel()

                continue

            status, count = info

            window["-PROCESSING-"].update_bar(status)

            sg.Print(status, count)

            ret = sg.OneLineProgressMeter(

                'Byte カウンター', status, 100, "", '{}'.format(count))

            if not ret: # 進捗ダイアログの Cancel が押された時

                task_cancel()

                continue


まずは、フレーム要素を中央に配置したver2.0を作っていく