Tkinter-Monitor-Window 的类方法

发布于 2024-07-18 05:05:34 字数 1784 浏览 7 评论 0原文

我想实现一个监视窗口,向用户报告正在进行的计算。 为此,我写了一个小类。 但由于我想以一种简单的方式在不同的模块中使用它,我想用类方法来实现它。 这允许在没有实例的情况下按以下方式使用它:

from MonitorModule import Monitor
Monitor.write("xyz")

此外,如果我在其他模块中使用它,则 other_module.pyMonitor.write() 的输出将显示在同一窗口中。

我可以在每个模块中导入它,以将特定输出重定向到同一监视器。 除了一件我不明白的小事之外,我让它工作了。 我无法使用我编写的特定处理程序关闭监视器窗口。 我可以使用非类方法来完成此操作,但不能使用处理程序作为类方法。

看一下代码:

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

所以,类方法似乎可以工作,并且似乎可以以正确的方式访问! 你知道哪里出了问题吗?它不能与按钮一起使用吗?

干杯,菲利普

I would like to realise a monitor window that reports the user about ongoing computations. To do so I wrote a little class. But as I would like to use it accross different modules in an easy fashion I thought to implement it with classmethods. This allows to use it in the following way without instances:

from MonitorModule import Monitor
Monitor.write("xyz")

Also, if I use it in an other module, the output of Monitor.write() within other_module.py will be displayed in the same window.

This I can import in each module to redirect specific outputs to the same monitor. I got it to work except one little thing that I don't understand. I can't close the Monitor-window with the specific handler that I wrote. I could do it with the non-classmethod-way but not with the handler as a classmethod.

Look at the code:

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

So, the classmethod seems to work and also seems to be accessible in the right way! Any idea, what went wrong, that it doesn't work with the button?

Cheers, Philipp

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

国产ˉ祖宗 2024-07-25 05:05:34

您不需要大量的类方法只是为了轻松地跨多个模块使用对象。

相反,请考虑在模块导入时创建一个实例,如下所示:

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

You don't need lots of classmethods just to make it easy to use an object across multiple modules.

Instead consider making an instance at module import time as shown here:

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")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文