classmethodのためのTkinterモニタウィンドウ
-
12-09-2019 - |
質問
たいと思いを実現させるモニターウィンドウ内に報告書は、ユーザは、現在も継続して行計算.なんの少しクラスです。したが、とても使いたいと思うので挟異なるモジュールやファッションだと思いプロジェクトを実施するとclassmethods.この方法に使用するのは以下のような場合:
from MonitorModule import Monitor
Monitor.write("xyz")
また、私は利用し他のモジュールの出力 モニターwrite() 以内 other_module.py 表示されますが同じウインドウです。
これかをインポートすることができ、各モジュールへのリダイレクト固有のアウトプットと同じ。ログインしてくださる以外の作業はさすがに調度品等は古くなったものかわからない.できないのモニター-ウィンドウのハンドラを書いた.しかできなかったんで、non-classmethod方がないと、ハンドラとしてclassmethod.
のコード:
import Tkinter
class Monitor_non_classmothod_way(object):
def __init__(self):
self.mw = Tkinter.Tk()
self.mw.title("Messages by NeuronSimulation")
self.text = Tkinter.Text(self.mw, width = 80, height = 30)
self.text.pack()
self.mw.protocol(name="WM_DELETE_WINDOW", func=self.handler)
self.is_mw = True
def write(self, s):
if self.is_mw:
self.text.insert(Tkinter.END, str(s) + "\n")
else:
print str(s)
def handler(self):
self.is_mw = False
self.mw.quit()
self.mw.destroy()
class Monitor(object):
@classmethod
def write(cls, s):
if cls.is_mw:
cls.text.insert(Tkinter.END, str(s) + "\n")
else:
print str(s)
@classmethod
def handler(cls):
cls.is_mw = False
cls.mw.quit()
cls.mw.destroy()
mw = Tkinter.Tk()
mw.title("Messages by NeuronSimulation")
text = Tkinter.Text(mw, width = 80, height = 30)
text.pack()
mw.protocol(name="WM_DELETE_WINDOW", func=handler)
close = handler
is_mw = True
a = Monitor_non_classmothod_way()
a.write("Hello Monitor one!")
# click the close button: it works
b = Monitor()
Monitor.write("Hello Monitor two!")
# click the close button: it DOESN'T work, BUT:
# >>> Monitor.close()
# works...
そのclassmethodうもうするの!あらゆるアイデア、ビジネスを行なっているいかないんで、ボタン?
声でフィリップ-ハーダー氏
解決
必要な多くのclassmethodsうとしてお使いいただけるようにオブジェクトを複数のモジュールです。
代わりに考えてインスタンスはモジュールの輸入時に明示的に定義しています。
import Tkinter
class Monitor(object):
def __init__(self):
self.mw = Tkinter.Tk()
self.mw.title("Messages by NeuronSimulation")
self.text = Tkinter.Text(self.mw, width = 80, height = 30)
self.text.pack()
self.mw.protocol(name="WM_DELETE_WINDOW", func=self.handler)
self.is_mw = True
def write(self, s):
if self.is_mw:
self.text.insert(Tkinter.END, str(s) + "\n")
else:
print str(s)
def handler(self):
self.is_mw = False
self.mw.quit()
self.mw.destroy()
monitor = Monitor()
other_module.py
from monitor import monitor
monitor.write("Foo")
所属していません StackOverflow