import wx
from NormalLogger import NormalLogger
from SingletonLogger import SingletonLogger
class LogApp(wx.Frame):
def __init__(self, parent, title):
super(LogApp, self).__init__(parent, title=title, size=(1000, 800))
self.panel = wx.Panel(self)
self.normal_loggers = []
self.init_ui()
def init_ui(self):
sizer = wx.BoxSizer(wx.HORIZONTAL)
# 左边单例模式
left_sizer = wx.BoxSizer(wx.VERTICAL)
self.create_singleton_button1 = wx.Button(self.panel, label="创建单例对象1")
self.create_singleton_button1.Bind(wx.EVT_BUTTON, self.on_create_singleton1)
self.create_singleton_button2 = wx.Button(self.panel, label="创建单例对象2")
self.create_singleton_button2.Bind(wx.EVT_BUTTON, self.on_create_singleton2)
self.clear_singleton_button = wx.Button(self.panel, label="清除日志文件")
self.clear_singleton_button.Bind(wx.EVT_BUTTON, self.on_clear_singleton)
top_buttons_sizer = wx.BoxSizer(wx.HORIZONTAL)
top_buttons_sizer.Add(self.create_singleton_button1, 0, wx.ALL, 5)
top_buttons_sizer.Add(self.create_singleton_button2, 0, wx.ALL, 5)
top_buttons_sizer.Add(self.clear_singleton_button, 0, wx.ALL, 5)
left_sizer.Add(top_buttons_sizer, 0, wx.ALL | wx.EXPAND, 5)
self.singleton_log_input = wx.TextCtrl(self.panel, style=wx.TE_MULTILINE)
left_sizer.Add(self.singleton_log_input, 1, wx.ALL | wx.EXPAND, 5)
self.write_singleton_button = wx.Button(self.panel, label="写日志")
self.write_singleton_button.Bind(wx.EVT_BUTTON, self.on_write_singleton)
left_sizer.Add(self.write_singleton_button, 0, wx.ALL, 5)
self.singleton_log_output = wx.TextCtrl(self.panel, style=wx.TE_READONLY | wx.TE_MULTILINE)
left_sizer.Add(self.singleton_log_output, 1, wx.ALL | wx.EXPAND, 5)
self.read_singleton_button = wx.Button(self.panel, label="读日志")
self.read_singleton_button.Bind(wx.EVT_BUTTON, self.on_read_singleton)
left_sizer.Add(self.read_singleton_button, 0, wx.ALL, 5)
sizer.Add(left_sizer, 1, wx.ALL | wx.EXPAND, 5)
# 右边普通多实例模式
right_sizer = wx.BoxSizer(wx.VERTICAL)
self.create_normal_button1 = wx.Button(self.panel, label="创建普通对象1")
self.create_normal_button1.Bind(wx.EVT_BUTTON, self.on_create_normal1)
self.create_normal_button2 = wx.Button(self.panel, label="创建普通对象2")
self.create_normal_button2.Bind(wx.EVT_BUTTON, self.on_create_normal2)
self.clear_normal_button = wx.Button(self.panel, label="清除日志文件")
self.clear_normal_button.Bind(wx.EVT_BUTTON, self.on_clear_normal)
top_buttons_sizer = wx.BoxSizer(wx.HORIZONTAL)
top_buttons_sizer.Add(self.create_normal_button1, 0, wx.ALL, 5)
top_buttons_sizer.Add(self.create_normal_button2, 0, wx.ALL, 5)
top_buttons_sizer.Add(self.clear_normal_button, 0, wx.ALL, 5)
right_sizer.Add(top_buttons_sizer, 0, wx.ALL | wx.EXPAND, 5)
self.normal_log_input = wx.TextCtrl(self.panel, style=wx.TE_MULTILINE)
right_sizer.Add(self.normal_log_input, 1, wx.ALL | wx.EXPAND, 5)
self.write_normal_button = wx.Button(self.panel, label="写日志")
self.write_normal_button.Bind(wx.EVT_BUTTON, self.on_write_normal)
right_sizer.Add(self.write_normal_button, 0, wx.ALL, 5)
self.normal_log_output = wx.TextCtrl(self.panel, style=wx.TE_READONLY | wx.TE_MULTILINE)
right_sizer.Add(self.normal_log_output, 1, wx.ALL | wx.EXPAND, 5)
self.read_normal_button = wx.Button(self.panel, label="读日志")
self.read_normal_button.Bind(wx.EVT_BUTTON, self.on_read_normal)
right_sizer.Add(self.read_normal_button, 0, wx.ALL, 5)
sizer.Add(right_sizer, 1, wx.ALL | wx.EXPAND, 5)
self.panel.SetSizer(sizer)
def on_clear_singleton(self, event):
SingletonLogger.clear_log()
self.singleton_log_output.SetValue("")
def on_create_singleton1(self, event):
self.singleton_logger = SingletonLogger()
wx.MessageBox("单例对象1已创建", "信息")
def on_create_singleton2(self, event):
self.singleton_logger = SingletonLogger()
wx.MessageBox("单例对象2已创建", "信息")
def on_write_singleton(self, event):
message = self.singleton_log_input.GetValue()
if message:
self.singleton_logger.write_log(message)
self.singleton_log_input.SetValue("")
def on_read_singleton(self, event):
log_content = self.singleton_logger.read_log()
self.singleton_log_output.SetValue(log_content)
def on_clear_normal(self, event):
NormalLogger.clear_log()
self.normal_log_output.SetValue("")
def on_create_normal1(self, event):
self.normal_loggers.append(NormalLogger())
wx.MessageBox("普通对象1已创建", "信息")
def on_create_normal2(self, event):
self.normal_loggers.append(NormalLogger())
wx.MessageBox("普通对象2已创建", "信息")
def on_write_normal(self, event):
message = self.normal_log_input.GetValue()
if message and self.normal_loggers:
self.normal_loggers[-1].write_log(message)
self.normal_log_input.SetValue("")
def on_read_normal(self, event):
if self.normal_loggers:
log_content = self.normal_loggers[-1].read_log()
self.normal_log_output.SetValue(log_content)
else:
wx.MessageBox("没有创建任何普通对象", "警告")
if __name__ == "__main__":
app = wx.App(False)
frame = LogApp(None, "单例模式与普通多实例模式对比示例")
frame.Show()
app.MainLoop()