質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

Q&A

解決済

1回答

1581閲覧

python, gui, tkinter.after, global宣言をなくしたい

yamatail

総合スコア77

Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

0グッド

0クリップ

投稿2020/03/10 00:52

pythonのguiモジュールのtkinterで
フォルダを監視する処理をtkinter.afterを用いて作成しております。

一応、形にできたのですが、一つ懸念材料があります。

プログラムにはあまりglobalを使わない方が良いと聞いたことが御座いまして、
どうにかglobalを使わずに以下の処理を完了したいのですが、
どうにもできません。

具体的に言いますと
今は、global関数に繰り返し回数を示すrepet_numsとafter_idを使っていますが、
これのglobal宣言をせず、def間で変数を渡せる方法はないものかと考えております。

「出来てるからええやん。」と言われればそれまでなのですが、
知識を増やすためにもどうかご教授の程よろしくお願い致します。

<globalを外すと出てくエラーメッセージ>
local variable 'repet_nums' referenced before assignment
とか、
stopボタンが効かなくなる
など

<開発環境>
python3
windows10 64bit

python

1# -*- coding: utf-8 -*- 2""" 3Created on Mon Mar 2 15:00:20 2020 4""" 5 6import tkinter as tk 7 8def main(): 9 Main.convert() 10 11class Main(): 12 def convert(): 13 #after_id = None 14 update_intarval = 1000 #(msec) 15 watch_mode = 1 #監視モード 16 17 def convert_data(): 18 global after_id 19 global repet_nums 20 repet_nums += 1 21 22 for nraw in range(10): 23 pass ## メイン処理 24 25 after_id = showinfo.after(update_intarval, convert_data) 26 if watch_mode == 0: 27 stop_convert() 28 print(repet_nums,after_id) 29 30 def start(): 31 global repet_nums 32 repet_nums = 0 33 convert_data() 34 35 def stop_convert(): 36 global after_id 37 print('push on') 38 print(after_id) 39 if after_id: 40 showinfo.after_cancel(after_id) 41 print(after_id) 42 after_id = None 43 print(after_id) 44 showinfo.destroy() 45 46 showinfo = tk.Tk() 47 frame_bottom = tk.Frame(showinfo,bd=2) 48 frame_bottom.pack(fill="x") 49 50 label2 = tk.Label(frame_bottom, text="STOPボタンで停止  --->") 51 label2.pack(fill="x", padx=10, side="left") 52 53 button1 = tk.Button(frame_bottom, text="STOP", width=15, bg="#f0e68c", fg="#ff0000", command=stop_convert) 54 button1.pack(fill="x", padx=10, side="left") 55 56 start() 57 showinfo.mainloop() 58 59if __name__ == '__main__': 60 main()

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

単純にメンバ変数にするだけなら以下のような感じでしょうか。

python

1# -*- coding: utf-8 -*- 2 3import tkinter as tk 4 5class Main(): 6 def __init__(self,tki): 7 self.after_id = None 8 self.repet_nums = 0 9 self.watch_mode = 1 #監視モード 10 self.update_intarval = 1000 #(msec) 11 self.showinfo = tki 12 13 def convert_data(self): 14 self.repet_nums += 1 15 16 for nraw in range(10): 17 pass ## メイン処理 18 19 self.after_id = self.showinfo.after(self.update_intarval, self.convert_data) 20 if self.watch_mode == 0: 21 self.stop_convert() 22 print(self.repet_nums,self.after_id) 23 24 def start(self): 25 self.repet_nums = 0 26 self.convert_data() 27 28 def stop_convert(self): 29 after_id = self.after_id 30 self.after_id = None 31 print('push on') 32 print(after_id) 33 if after_id: 34 self.showinfo.after_cancel(after_id) 35 print(after_id) 36 print(self.after_id) 37 self.showinfo.destroy() 38 39def main(): 40 41 showinfo = tk.Tk() 42 frame_bottom = tk.Frame(showinfo,bd=2) 43 frame_bottom.pack(fill="x") 44 45 label2 = tk.Label(frame_bottom, text="STOPボタンで停止  --->") 46 label2.pack(fill="x", padx=10, side="left") 47 48 cnv = Main(showinfo) 49 50 button1 = tk.Button(frame_bottom, text="STOP", width=15, bg="#f0e68c", fg="#ff0000", command=cnv.stop_convert) 51 button1.pack(fill="x", padx=10, side="left") 52 53 cnv.start() 54 showinfo.mainloop() 55 56if __name__ == '__main__': 57 main() 58

投稿2020/03/10 01:37

t_obara

総合スコア5488

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

yamatail

2020/03/10 02:29

t_obara様 おー思った通りの動作がglobal変数を使わずに実現しました。 ありがとうございます!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問