LoginSignup
126
157

PythonのデスクトップアプリをGUI操作で作りたかった

Last updated at Posted at 2024-01-09

最初に

PythonでGUIのアプリを作ろうと探した所、Tkinterという物で作成出来ることを知りました。

ただ画面サイズからプロパティまでコードベースでやらなければならないため、非常に時間が掛かります。

そこでVisual Studioの操作みたいに作れるツールが無いか探した所、発見したのでお伝えさせてさせて頂きます。

Python GUI 開発ツール「PAGE」

インストール手順

下記ぺージからダウンロードできます。

「Download Now」をクリックします。

image.png

カウントが0になるとファイルが表示されるので、ダウンロードを行います。

image.png

ダウンロードしたファイルを起動すると以下が表示されるので「はい」を選択します。

image.png

「Next」をクリックします。

image.png

配置場所を指定するのですが任意で設定します。

image.png

「はい」を選択します。

image.png

「Next」をクリックします。

image.png

「Install」をクリックします。

image.png

これでインストール完了です。

操作方法

インストールしたフォルダの「page.py」を実行します。
image.png

起動すると複数画面が表示されます。
image.png

後はVisual Studioの.NETの画面開発する勢いで画面に配置して、イベント発火時のコードを仕込めば実装出来そうです。

内部にサンプルアプリケーションがありました。

image.png

下記の例だとbind_example.pyを実行すると画面が表示されます。

image.png

Buttonを押すと後ろに文字列が出力されます。

image.png

関連しているファイルは二つありコードは以下でした。

bind_example.py
#! /usr/bin/env python
#  -*- coding: utf-8 -*-
#
# GUI module generated by PAGE version 7.5e
#  in conjunction with Tcl version 8.6
#    May 28, 2022 07:45:04 AM PDT  platform: Linux

import sys
import tkinter as tk
import tkinter.ttk as ttk
from tkinter.constants import *
import os.path

_script = sys.argv[0]
_location = os.path.dirname(_script)

import bind_example_support

_bgcolor = 'wheat'  # X11 color: #f5deb3
_fgcolor = '#000000'  # X11 color: 'black'
_compcolor = '#b2c9f4' # Closest X11 color: 'SlateGray2'
_ana1color = '#eaf4b2' # Closest X11 color: '{pale goldenrod}'
_ana2color = '#f4bcb2' # Closest X11 color: 'RosyBrown2'
_tabfg1 = 'black' 
_tabfg2 = 'black' 
_tabbg1 = 'grey75' 
_tabbg2 = 'grey89' 
_bgmode = 'light' 

class Toplevel1:
    def __init__(self, top=None):
        '''This class configures and populates the toplevel window.
           top is the toplevel containing window.'''

        top.geometry("600x450+617+339")
        top.minsize(1, 1)
        top.maxsize(1905, 1170)
        top.resizable(0,  0)
        top.title("New Toplevel 1")
        top.configure(background="wheat")
        top.configure(highlightbackground="wheat")
        top.configure(highlightcolor="black")

        self.top = top

        self.Button1 = tk.Button(self.top)
        self.Button1.place(x=240, y=210, height=33, width=81)
        self.Button1.configure(activebackground="#f4bcb2")
        self.Button1.configure(background="wheat")
        self.Button1.configure(command=bind_example_support.button_handler1)
        self.Button1.configure(disabledforeground="#b8a786")
        self.Button1.configure(font="-family {DejaVu Sans} -size 12")
        self.Button1.configure(highlightbackground="wheat")
        self.Button1.configure(text='''Button''')
        self.Button1.bind('<Button-2>',lambda e:bind_example_support.button_handler2(e,2))
        self.Button1.bind('<Button-3>',lambda e:bind_example_support.button_handler2(e,3))

        self.Button2 = tk.Button(self.top)
        self.Button2.place(x=240, y=310, height=33, width=61)
        self.Button2.configure(activebackground="#f4bcb2")
        self.Button2.configure(background="wheat")
        self.Button2.configure(command=bind_example_support.quit)
        self.Button2.configure(disabledforeground="#b8a786")
        self.Button2.configure(font="-family {DejaVu Sans} -size 12")
        self.Button2.configure(highlightbackground="wheat")
        self.Button2.configure(text='''Quit''')

def start_up():
    bind_example_support.main()

if __name__ == '__main__':
    bind_example_support.main()
bind_example_support.py
#! /usr/bin/env python
#  -*- coding: utf-8 -*-
#
# Support module generated by PAGE version 7v
#  in conjunction with Tcl version 8.6
#    Sep 12, 2021 09:07:09 AM PDT  platform: Linux

import sys
import tkinter as tk
import tkinter.ttk as ttk
from tkinter.constants import *

import bind_example

def main(*args):
    '''Main entry point for the application.'''
    global root
    root = tk.Tk()
    root.protocol( 'WM_DELETE_WINDOW' , root.destroy)
    # Creates a toplevel widget.
    global _top37, _w37
    _top37 = root
    _w37 = bind_example.Toplevel1(_top37)
    root.mainloop()

def button_handler1(*args):
    print('bind_example_support.button_handler1')
    for arg in args:
        print ('another arg:', arg)
    sys.stdout.flush()

def button_handler2(*args):
    print('bind_example_support.button_handler2')
    for arg in args:
        print ('another arg:', arg)
    sys.stdout.flush()

def quit(*args):
    print('bind_example_support.quit')
    for arg in args:
        print ('another arg:', arg)
    sys.stdout.flush()
    sys.exit()

if __name__ == '__main__':
    bind_example.start_up()

画面の構成はbind_example.pyで指定し、bind_example_support.pyでボタンから実行するコード内容を記載するような構成です。これであれば簡単にタイマーを使った実行処理等の実装が出来そうです。

Udemyで検索した所、Tkinterに関する講座がありました。

受講してみた感想ですが、中々のコーディング量が必要にはなりますがPythonだけで全て作成できるので学習コストは高めですが応用が効きそうでした。

image.png

簡単ではありますがこちらで以上となります。
見て頂きありがとうございました。

以下参考にさせて頂いたサイト様です。

126
157
1

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
126
157