一つのボタンで複数関数を動かしたいことが有ります。
❶ まず分かり易い初心者向けcodeです。
# 一つのボタンで複数関数を動かす1.py 10/1/2020 import tkinter as tk root = tk.Tk() root.geometry(“200×200”) labelA = tk.Label(root, text=”写真を表示する”) labelA.place(x=30, y=110) labelB = tk.Label(root, text=”音楽を鳴らす”) labelB.place(x=30, y=90) labelC = tk.Label(root, text=”codeを表示する”) labelC.place(x=30, y=70) button = tk.Button(root, text=”次の三つの処理を行います”) button.place(x=30, y=20) def funcA(): labelA[“text”] = “写真を表示しました” def funcB(): labelB[“text”] = “Jazzを演奏中です” funcA() def funcC(event): labelC[“text”] = “ラベル文字変更codeは次です” funcB() button.bind(“<Button>” , funcC) root.mainloop() |
左が処理前、右が処理後。 実際はこの後にプログラムを続ける。
❷ プロっぽいプログラム
結果は上と同じになります。 オブジェクト思考に慣れている方にはこの方がいいのでしょうね。
try: import Tkinter as tk except: import tkinter as tk class Test(): def __init__(self): self.root = tk.Tk() self.root.geometry(‘200×100’) self.button = tk.Button(self.root, text = ‘次の三つの処理を行います’, command=lambda:[self.funcA(), self.funcB(), self.funcC()]) self.button.pack() self.labelA = tk.Label(self.root, text=”写真を表示する”) self.labelB = tk.Label(self.root, text=”音楽を鳴らす”) self.labelC = tk.Label(self.root, text=”codeを表示する”) self.labelA.pack() self.labelB.pack() self.labelC.pack() self.root.mainloop() def funcA(self): self.labelA[“text”] = “写真を表示しました” def funcB(self): self.labelB[“text”] = “Jazzを演奏中です” def funcC(self): self.labelC[“text”] = “ラベル文字変更codeは次です” app = Test() |