LoginSignup
0
2

More than 1 year has passed since last update.

python学習のアウトプット

Posted at

私がこれまでに学習したpythonの知識をここにアウトプットします。初回である今回はテキストで学んだことを実際に私自身が作成したアプリのプログラムを掲載して、投稿させていただきます。

作成したアプリは名付けて、「ファイル起動・整理APP」です。自身のPCにてよく使用するファイルへのショートカット及びファイル移動なども可能にしたアプリケーションです。それでは実際のアプリケーションとコードを下に掲載いたします。

メニュー画面(実行結果)
image.png
コード全体

インポート

import tkinter as tk
from tkinter import ttk
from tkinter import messagebox
from tkinter import filedialog
import subprocess
import os
import shutil

まず、APPを動かすのに必要な上記モジュールをインポートします。
関数を使用しサブウインドウ1を記述します。

関数

サブウィンドウGUI

def sub_window1():
# フォルダ・ファイル選択画面の作成(サブウィンドウ1)
mini_window1 = tk.Toplevel(root)
mini_window1.title("フォルダ・ファイル選択画面")

# ラベルフレーム1
frm1 = ttk.Labelframe(mini_window1,text = "フォルダ・ファイル選択",
                     padding = 10)
frm1.grid(row = 0,column = 0,padx = 20,pady = 15)

# ラジオボタン
radio_value = tk.IntVar()

### 選択画面における条件分岐 ###

def read_func():
    if radio_value.get() == 1:
        subprocess.Popen(["explorer",
              r"C:\Users\h_mat\Documents\就職活動"]
                     ,shell = True)

    elif radio_value.get() == 2:
        subprocess.Popen(["start",
                  r"C:\Users\h_mat\Documents\その他\家計簿\家計簿.xlsm"]
                         ,shell = True)

    elif radio_value.get() == 3:
        subprocess.Popen(["explorer",
                  r"C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe"]
                         ,shell = True)

    elif radio_value.get() == 4:
        subprocess.Popen(["explorer",r"C:\Users\h_mat\Documents\python"]
                         ,shell = True)
    
    # 例外処理
    else:
        messagebox.showwarning("エラー","フォルダまたはファイルを選択してください")



# ラジオボタン1
rdio_1 = ttk.Radiobutton(frm1,text = "就職活動",
                         variable = radio_value, value = 1)
rdio_1.grid(row = 0,column = 0,
            padx = 20,ipadx = 20,pady = 5,
            ipady = 5,sticky = tk.W)

# ラジオボタン2
rdio_2 = ttk.Radiobutton(frm1,text = "家計簿",
                        variable = radio_value,value = 2)
rdio_2.grid(row = 0,column = 1,
            padx = 20,ipadx = 10,pady = 5,
            ipady = 5,sticky = tk.W)

# ラジオボタン3
rdio_3 = ttk.Radiobutton(frm1,text = "ブラウザ",
                        variable = radio_value,value = 3)
rdio_3.grid(row = 1,column = 0,
            padx = 20,ipadx = 20,pady = 5,
            ipady = 5,sticky = tk.W)

# ラジオボタン4
rdio_4 = ttk.Radiobutton(frm1,text = "python",
                        variable = radio_value,value = 4)
rdio_4.grid(row = 1,column = 1,
            padx = 20,ipadx = 10,pady = 5,
            ipady = 5,sticky = tk.W)

# 実行ボタン
button5 = tk.Button(mini_window1,text = "実行",
                    command = lambda:read_func())
button5.grid(row = 3,column = 0,pady = 10)

次にサブウィンドウ2を作成します。

def sub_window2():

# ウインドウの作成
mini_window2 = tk.Toplevel(root)
mini_window2.title("ファイル整理画面") 

# ラベルフレーム2
frm2 = ttk.Labelframe(mini_window2,text = "ファイル整理",
                     padding = 10)
frm2.grid(row = 0,column = 0,padx = 20,pady = 15)

### ファイル移動の関数 ###
# 参照
def browse_func():
    #グローバル変数の宣言
    global dir_path
    # 整理するフォルダのパス
    dir_path = filedialog.askdirectory()
# 移動
def run_func():
    try:
        # 移動先フォルダのパス
        save_path = filedialog.askdirectory()
        # 拡張子の判別とファイル移動
        for file in os.listdir(dir_path):
            if file.endswith(cb.get()):
                path = dir_path + "/" + file
                shutil.move(path,save_path)
    except:
        pass

# コンボボックス
extensions = [".docx",".xlsx",".xlsm",".txt",".py",".jpg"]
cb = ttk.Combobox(frm2,values = extensions,state = "readonly")
cb.pack(side = tk.LEFT)
cb.current(0)

# 移動ボタン
button6 = tk.Button(mini_window2,text = "移動",
                   command = run_func)
button6.grid(row = 2,column = 0,padx = 20,pady = 20,ipadx = 30)

# 参照ボタン
button7 = tk.Button(mini_window2,text = "参照",
                   command = browse_func)
button7.grid(row = 1,column = 0,padx = 20,ipadx = 30)

mini_window2.mainloop()

def sub_window3():

# ウインドウの作成
mini_window3 = tk.Toplevel(root)
mini_window3.title("フォルダ削除画面") 

# ラベルフレーム3
frm3 = ttk.Labelframe(mini_window3,text = "フォルダ削除",
                     padding = 10)
frm3.grid(row = 1,column = 1,padx = 20,pady = 15)

### フォルダ削除の関数 ###
def delete_func():
    try:
        #グローバル変数の宣言
        global delete_path
        # 整理するフォルダのパス
        delete_path = filedialog.askdirectory()
        # フォルダの削除
        shutil.rmtree(delete_path)
    except:
        pass
    
# 削除ボタン
button7 = tk.Button(frm3,text = "削除",command = delete_func)
button7.grid(row = 0,column = 0,padx = 20,ipadx = 30)

mini_window3.mainloop()

メインウインドウを作成していきます。

メインウインドウ関数

def exit_func():
# ウインドウを閉じる
root.destroy()

メインウィンドウGUI

メインウインドウの作成

root = tk.Tk()
root.title("ファイル起動・整理APP")

ラベルフレーム

frame = ttk.Labelframe(root,text = "メインメニュー",padding = 10)
frame.grid(row = 0,column = 0,padx = 20,pady = 15)

メニューボタン

APP・ファイル起動

button1 = tk.Button(frame,text = "APP・ファイル起動",command = sub_window1)
button1.grid(row = 0,column = 0,padx = 5)

ファイル整理

button2 = tk.Button(frame,text = " ファイル整理 ",command = sub_window2)
button2.grid(row = 0,column = 1,padx = 5)

APP・ファイル起動

button3 = tk.Button(frame,text = " フォルダ削除 ",command = sub_window3)
button3.grid(row = 0,column = 2,padx = 5)

終了ボタン

button4 = tk.Button(frame,text = " APP終了 ",command = exit_func)
button4.grid(row = 1,column = 1,padx = 5,pady = 20,ipadx = 30)

root.mainloop()

以上になります。次回はコードについての解説を入れていきます。

エンジニアファーストの会社 株式会社CRE-CO H_M

0
2
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
0
2