私がPythonを用いるときの目的は文字列の加工なのですが、いつもCUIで実施していたのでGUIで出来たら良いなと思っていました。
今回は私の目的においては色々と使いまわせるtkinterを用いたGUIプログラムの雛形を作成しました。
createボタンをクリックすると、テキストボックスに入力された文字列がinput_str_procで加工された新しいウィンドウのテキストボックスで出力されます。
import tkinter as tk
import tkinter.messagebox as mbox
root = tk.Tk()
label = tk.Label(root, text="文字列処理の雛形")
label.pack()
# ボタンを格納するフレームを作成
frame = tk.Frame(root)
frame.pack()
def input_str_proc(string):
#テキストボックスに入力された文字列を加工する処理を記載する
return string
def create_new_window():
# 新しいウィンドウを作成
new_window = tk.Toplevel(root)
# Scrollbarを作成
sub_scrollbar = tk.Scrollbar(new_window)
sub_scrollbar.pack(side=tk.RIGHT, fill=tk.Y)
# Textウィジェットを作成し、初期テキストを設定
sub_text_box = tk.Text(new_window,yscrollcommand=sub_scrollbar.set)
# ScrollbarとTextウィジェットを紐づける
sub_scrollbar.config(command=sub_text_box.yview)
# Textウィジェットの幅と高さを設定
sub_text_box.config(width=50, height=10)
# Textウィジェットを配置
sub_text_box.pack(side = "bottom",fill=tk.BOTH, expand=True)
#テキストボックスから文字列を取得
input_str = text_box.get("1.0", "end-1c")
#入力文字列を加工
output_str = input_str_proc( input_str )
sub_text_box.insert(tk.END, output_str)
# Scrollbarを作成
scrollbar = tk.Scrollbar(root)
scrollbar.pack(side=tk.RIGHT, fill=tk.Y)
# Textウィジェットを作成し、初期テキストを設定
text_box = tk.Text(root,yscrollcommand=scrollbar.set,undo=True)
text_box.insert(tk.END, "Type your text here")
# ScrollbarとTextウィジェットを紐づける
scrollbar.config(command=text_box.yview)
# Textウィジェットの幅と高さを設定
text_box.config(width=50, height=10)
#テキストボックスのundoを実施。
def undo(event):
if event.state == 4 and event.keysym == "z":
text_box.edit_undo()
text_box.edit_modified(False)
text_box.bind("<Key>", undo)
# Textウィジェットを配置
text_box.pack(side="bottom",fill=tk.BOTH, expand=True)
def button_click4():
text_box.delete('1.0', tk.END)
text_box.insert(tk.END, insertMsg)
button4 = tk.Button(frame, text="create", command=create_new_window)
button4.pack(side="left")
root.mainloop()