概要
python tkinterを用いて、GUIで指定したファイルに対して処理を行うプログラムを記載することが多い。
基本的には、整数バーやラジオボタン等を使ってパラメータを指定し、ファイルを指定して出力、という流れがほとんどとなる。毎回書き方を調べるのも大変なので、良く使う項目をクラス化し、プログラム例としてまとめてみた。
スクリプト
python3.5.6にて動作を確認。
パラメータをセットし、ファイルを選択することでtest_functionの処理が走る仕組み。
基本的には、Flame_classのtest_functionとcreate_widgetsの部分を弄ることで、それなりのGUIプログラムが出来るはず。
from tkinter import *
import tkinter.ttk as ttk
import tkinter.filedialog as tkfd
import tkinter.messagebox as tkmb
import os
class tkgui_class():
# GUIの基本クラス
def __init__(self, frame, label, row, column):
self.frame = frame
self.label = label
self.row = row
self.column = column
class IntVar_class(tkgui_class):
# 整数入力バー
def __init__(self, frame, label, set_x, row, column,width = 15):
super().__init__(frame, label, row, column)
self.set_x = set_x
self.width = width
self.create_label()
def create_label(self):
self.label = ttk.Label(self.frame, text=self.label + ':')
self.ver = IntVar()
self.ver.set(self.set_x)
self.entry = ttk.Entry(self.frame, textvariable=self.ver, width = self.width)
self.label.grid(row=self.row, column=self.column, sticky=E)
self.entry.grid(row=self.row, column=self.column+1,sticky=W)
def get(self):
return self.ver.get()
class StringVar_class(IntVar_class):
# 文字入力バー
def __init__(self, frame, label, set_x, row, column,width = 25):
super().__init__(frame, label, set_x, row, column,width = width)
self.ver = StringVar()
self.ver.set(self.set_x)
self.entry = ttk.Entry(self.frame, textvariable=self.ver, width = self.width)
self.label.grid(row=self.row, column=self.column, sticky=E)
self.entry.grid(row=self.row, column=self.column+1,sticky=W)
class CheckButton_class(IntVar_class):
# チェックボタン
def __init__(self, frame, label, set_off, set_on, row, column,width = 13):
super().__init__(frame, label, 0, row, column,width = width)
self.set_on = set_on
self.set_off = set_off
self.ver = StringVar()
self.ver.set(self.set_off)
self.entry = ttk.Checkbutton(self.frame, textvariable=self.ver, width = self.width,onvalue=set_on,offvalue=set_off,variable=self.ver)
self.label.grid(row=self.row, column=self.column, sticky=E)
self.entry.grid(row=self.row, column=self.column+1,sticky=W)
class Frame_class(ttk.Frame):
# 全体のボタン、バーの配置決定
def __init__(self, master=None):
super().__init__(master)
self.create_widgets()
def create_widgets(self):
self.master.grid(row=1,column=0,sticky=(N,E,S,W))
# "sample1", 10,1,1の部分で、順に、ラベル表示名、初期値、行、列を指定するだけ
self.sample1 = IntVar_class(self.master,"sample1", 10,1,1)
self.sample2 = IntVar_class(self.master,"sample2", 10,2,1)
self.sample3 = IntVar_class(self.master,"sample3", 20,3,1)
self.sample4 = IntVar_class(self.master,"sample4", 20,3,3)
self.sample5 = IntVar_class(self.master,"sample5", 10,4,1)
self.sample6 = IntVar_class(self.master,"sample6", 10,4,3)
self.checkbutton1 = CheckButton_class(self.master,"checkbutton1", "1", "2", 5,1)
self.checkbutton2 = CheckButton_class(self.master,"checkbutton2", "1", "2", 5,2)
self.outputfilename = StringVar_class(self.master,"Outputname", "filename.test",6,1)
self.button1 = ttk.Button(self.master, text='ファイル選択 & 実行', command=self.choice_file)
self.button1.grid(row=6,column=4,sticky=W)
# padding
for child in self.master.winfo_children():
child.grid_configure(padx=5, pady=3)
def choice_file(self):
fTyp=[('***ファイル','*.*')]
iDir='./'
self.filename = tkfd.askopenfilename(filetypes=fTyp,initialdir=iDir)
self.test_function(self.filename,self.sample1.get(),self.sample2.get(),self.sample3.get(),self.sample4.get(),self.sample5.get(),self.sample6.get(),self.outputfilename.get(),self.checkbutton1.get(),self.checkbutton2.get())
tkmb.showinfo('sucsess','実行成功')
def test_function(self,filename,sample1,sample2,sample3,sample4,sample5,sample6,outputfilename,checkbutton1,checkbutton2):
# ファイルに対する処理をここに入れる
inputpath,inputname = os.path.split(filename)
output_filename = inputpath + "/" + outputfilename
print (filename)
print (output_filename)
root = Tk()
root.title('sample gui')
root.resizable(0,0)
frame_num = ttk.Frame(root)
frame_num_class = Frame_class(master = frame_num)
root.mainloop()