0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

python GUI tcl/tk 1クラスの複数ファイル分割

Posted at

実行サンプル

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")

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?