【Python】tkinter で作ったGUI

バージョン

Python 3.10.6

ソースコード

import os
import tkinter
from tkinter import *
from tkinter import ttk
from tkinter import messagebox
from tkinter import filedialog
from PIL import Image, ImageTk
import pyautogui 


# ファイルダイアログ
def filedialog_clicked():
    fTyp = [("", "*")]
    iFile = os.path.abspath(os.path.dirname(__file__))
    iFilePath = filedialog.askopenfilename(filetype = fTyp, initialdir = iFile)
    entry1.set(iFilePath)
    
    
# ボタン押下
def execute_clicked():
    pyautogui.alert('x:' + entryAX.get() + ' y:' + entryAY.get() + ' w:' + entryAW.get() + ' h:' +  entryAH.get())


# メイン処理
if __name__ == '__main__':

    root = tkinter.Tk()
    root.title("Sample")

    # Frame(左側)の作成
    frameL01 = ttk.Frame(root, padding=1)
    frameL01.grid(row=1, column=1, sticky=NW)
    Label01 = ttk.Label(frameL01, text="テキスト1", padding=(5, 2))
    Label01.pack(side=LEFT)
    
    frame1 = ttk.Frame(root, padding=10)
    frame1.grid(row=2, column=1, sticky=NW)
    
    # テキストボックス
    entry1 = StringVar()
    fileEntry = ttk.Entry(frame1, textvariable=entry1, width=40)
    fileEntry.pack(side=LEFT)
    
    # 「参照」ボタン
    fileButton = ttk.Button(frame1, text="参照", command=filedialog_clicked)
    fileButton.pack(side=LEFT)
    
    frameL03 = ttk.Frame(root, padding=1)
    frameL03.grid(row=3, column=1, sticky=NW)
    
    # X
    XLabel = ttk.Label(frameL03, text="X:", padding=(5, 2))
    XLabel.pack(side=LEFT)
    entryAX = StringVar()
    XEntry = ttk.Entry(frameL03, textvariable=entryAX, width=10)
    XEntry.insert(0,3)
    XEntry.pack(side=LEFT)

    # Y
    YLabel = ttk.Label(frameL03, text="Y:", padding=(5, 2))
    YLabel.pack(side=LEFT)
    entryAY = StringVar()
    YEntry = ttk.Entry(frameL03, textvariable=entryAY, width=10)
    YEntry.insert(0,0)
    YEntry.pack(side=LEFT)

    # W
    WLabel = ttk.Label(frameL03, text="W:", padding=(5, 2))
    WLabel.pack(side=LEFT)
    entryAW = StringVar()
    WEntry = ttk.Entry(frameL03, textvariable=entryAW, width=10)
    WEntry.insert(0,258)
    WEntry.pack(side=LEFT)
    
    # H
    HLabel = ttk.Label(frameL03, text="H:", padding=(5, 2))
    HLabel.pack(side=LEFT)
    entryAH = StringVar()
    HEntry = ttk.Entry(frameL03, textvariable=entryAH, width=10)
    HEntry.insert(0,156)
    HEntry.pack(side=LEFT)
    
    frameL04 = ttk.Frame(root, padding=10)
    frameL04.grid(row=4, column=1, sticky=NW)
    
    # ボタンの作成
    readButton = ttk.Button(frameL04, text="実行", command=execute_clicked)
    readButton.pack(side=LEFT)

    root.mainloop()

Leave a Reply

Your email address will not be published. Required fields are marked *

CAPTCHA