1
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.

簡単なPySimpleGUI①~二次方程式の解の公式~

Last updated at Posted at 2022-01-29

PysimpleGUIの使い方メモとして、二次方程式の解を「解の公式」で求めるGUIを作成しました。

この記事で扱う要素

入力
・数値入力ボックス(sg.InputText)
・ボタン (sg.Button, sg.B)

出力
・画面出力 (window['hoge'].update('hogehoge'))

コード

Code
import PySimpleGUI as sg

def main():
     layout =  [[sg.Text("解の公式GUI", size=(15, 1), justification='center', font=("Helvetica", 20), relief=sg.RELIEF_RIDGE)],
                [sg.T("ax^2+bx+c=0", size=(15, 1), justification='center')],
                [sg.Text('a :'), sg.InputText("1", key='a', size=(25, 1))],
                [sg.Text('b :'), sg.InputText("-4", key='b', size=(25, 1))],
                [sg.Text('c :'), sg.InputText("4", key='c', size=(25, 1))],
                [sg.Text('出力 :'), sg.Text("ここに答えが表示されます", key='ans')],
                [sg.Button('解の公式'), sg.B('Cancel')]]

    window = sg.Window('GUI window', layout)

    while True:
        event, values = window.read()
        a = float(values['a'])
        b = float(values['b'])
        c = float(values['c'])
        if event == None or event == 'Cancel':  # if user closes window or clicks cancel
            break

        elif event == '解の公式':
            ans = kai_no_kousiki(a, b, c)

        window['ans'].update(ans)

    window.close()

def kai_no_kousiki(a, b, c):
    D = (b**2 - 4*a*c) ** (1/2)
    x_1 = (-b + D) / (2 * a)
    x_2 = (-b - D) / (2 * a)

    return [x_1, x_2]

if __name__ == '__main__':
    main()

得られるGUI

image.png

1
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
1
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?