実行サンプル
tcl/tk で1つのクラスを複数ファイルに分割するサンプル。
1つのクラスが長文になった場合に可読性を良くしたいため。
実行環境
windows10
python3.9.5
pythonモジュール tcl/tk
実行手順
python プログラムファイル.py
ソースコード
main.py, fileB.py の2つのファイル
fileBの関数を呼び出すときは必ず self を引数で渡すようにする
main.py
import tkinter as tk
import fileB
class Application(tk.Frame):
def __init__(self, master=None):
super().__init__(master)
self.master = master
self.pack()
self.create_widgets()
def create_widgets(self):
self.entry1 = tk.Entry(self)
self.entry1.pack()
self.entry2 = tk.Entry(self)
self.entry2.pack()
self.button1 = tk.Button(self, text="Button 1", command=self.func_on_pushbutton1_click)
self.button1.pack()
self.button2 = tk.Button(self, text="Button 2", command=self.func_on_pushbutton2_click)
self.button2.pack()
def func_on_pushbutton1_click(self):
self.entry1.insert(0, "test1")
def func_on_pushbutton2_click(self):
fileB.zfunc_on_pushbutton2_click(self)
if __name__ == "__main__":
root = tk.Tk()
app = Application(master=root)
app.mainloop()
fileB.py
def zfunc_on_pushbutton2_click(self):
self.entry2.insert(0, "test2")