Skip to content

Commit

Permalink
Merge pull request #1 from Aareon/0.1.1
Browse files Browse the repository at this point in the history
0.1.2
  • Loading branch information
Aareon authored Sep 3, 2024
2 parents c08012d + ff2d9fc commit 025d31d
Show file tree
Hide file tree
Showing 6 changed files with 583 additions and 43 deletions.
98 changes: 98 additions & 0 deletions examples/grid_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import tkinter as tk
from tkinter import ttk
from loguru import logger
import sys

# Assuming the LoguruWidget class is in a file named loguru_widget.py
from tkloguru import LoguruWidget, setup_logger

class LoguruWidgetGridExample:
"""
An example application demonstrating the use of LoguruWidget with grid layout.
"""

def __init__(self, root):
"""
Initialize the example application.
Args:
root (tk.Tk): The root window of the application.
"""
self.root = root
self.root.title("LoguruWidget Grid Example")
self.root.geometry("800x600")

self.create_widgets()
self.setup_logging()

def create_widgets(self):
"""Create and arrange the widgets using grid layout."""
self.root.grid_columnconfigure(0, weight=1)
self.root.grid_rowconfigure(0, weight=1)
self.root.grid_rowconfigure(1, weight=0)

# Create a frame for the log widget
log_frame = ttk.Frame(self.root)
log_frame.grid(row=0, column=0, sticky="nsew", padx=10, pady=(10, 5))
log_frame.grid_columnconfigure(0, weight=1)
log_frame.grid_rowconfigure(0, weight=1)

# Create the LoguruWidget
self.log_widget = LoguruWidget(log_frame, show_scrollbar=True, color_mode='level', max_lines=1000)
self.log_widget.grid(row=0, column=0, sticky="nsew")

# Create a frame for buttons
button_frame = ttk.Frame(self.root)
button_frame.grid(row=1, column=0, sticky="ew", padx=10, pady=5)
button_frame.grid_columnconfigure((0, 1, 2), weight=1)

# Create buttons
generate_logs_button = ttk.Button(button_frame, text="Generate Sample Logs", command=self.generate_sample_logs)
generate_logs_button.grid(row=0, column=0, padx=5)

change_color_mode_button = ttk.Button(button_frame, text="Change Color Mode", command=self.change_color_mode)
change_color_mode_button.grid(row=0, column=1, padx=5)

change_log_level_button = ttk.Button(button_frame, text="Change Log Level", command=self.change_log_level)
change_log_level_button.grid(row=0, column=2, padx=5)

def setup_logging(self):
"""Set up the logger to use the LoguruWidget."""
setup_logger(self.log_widget)
logger.add(sys.stdout, level="DEBUG") # Add console handler for debugging

def generate_sample_logs(self):
"""Generate sample log messages for demonstration purposes."""
logger.debug("This is a debug message")
logger.info("This is an info message")
logger.success("This is a success message")
logger.warning("This is a warning message")
logger.error("This is an error message")
logger.critical("This is a critical message")

def change_color_mode(self):
"""Change the color mode of the log widget."""
current_mode = self.log_widget.color_mode
new_mode = 'full' if current_mode == 'level' else 'level' if current_mode == 'message' else 'message'
self.log_widget.color_mode = new_mode

logger.info(f"Changed color mode to: {new_mode}")

def change_log_level(self):
"""Change the logging level of the log widget."""
current_level = self.log_widget.get_logging_level()
levels = ["TRACE", "DEBUG", "INFO", "SUCCESS", "WARNING", "ERROR", "CRITICAL"]
current_index = levels.index(current_level)
new_index = (current_index + 1) % len(levels)
new_level = levels[new_index]

self.log_widget.set_logging_level(new_level)
logger.log(new_level, f"Changed logging level from {current_level} to: {new_level}")

def main():
root = tk.Tk()
app = LoguruWidgetGridExample(root)
root.mainloop()

if __name__ == "__main__":
main()
91 changes: 91 additions & 0 deletions examples/pack_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import tkinter as tk
from tkinter import ttk
from loguru import logger
import sys

# Assuming the LoguruWidget class is in a file named loguru_widget.py
from tkloguru import LoguruWidget, setup_logger

class LoguruWidgetPackExample:
"""
An example application demonstrating the use of LoguruWidget with pack layout.
"""

def __init__(self, root):
"""
Initialize the example application.
Args:
root (tk.Tk): The root window of the application.
"""
self.root = root
self.root.title("LoguruWidget Pack Example")
self.root.geometry("800x600")

self.create_widgets()
self.setup_logging()

def create_widgets(self):
"""Create and arrange the widgets using pack layout."""
# Create a frame for the log widget
log_frame = ttk.Frame(self.root)
log_frame.pack(expand=True, fill=tk.BOTH, padx=10, pady=(10, 5))

# Create the LoguruWidget
self.log_widget = LoguruWidget(log_frame, show_scrollbar=True, color_mode='level', max_lines=1000)
self.log_widget.pack(expand=True, fill=tk.BOTH)

# Create a frame for buttons
button_frame = ttk.Frame(self.root)
button_frame.pack(fill=tk.X, padx=10, pady=5)

# Create buttons
generate_logs_button = ttk.Button(button_frame, text="Generate Sample Logs", command=self.generate_sample_logs)
generate_logs_button.pack(side=tk.LEFT, padx=5)

change_color_mode_button = ttk.Button(button_frame, text="Change Color Mode", command=self.change_color_mode)
change_color_mode_button.pack(side=tk.LEFT, padx=5)

change_log_level_button = ttk.Button(button_frame, text="Change Log Level", command=self.change_log_level)
change_log_level_button.pack(side=tk.LEFT, padx=5)

def setup_logging(self):
"""Set up the logger to use the LoguruWidget."""
setup_logger(self.log_widget)
logger.add(sys.stdout, level="DEBUG") # Add console handler for debugging

def generate_sample_logs(self):
"""Generate sample log messages for demonstration purposes."""
logger.debug("This is a debug message")
logger.info("This is an info message")
logger.success("This is a success message")
logger.warning("This is a warning message")
logger.error("This is an error message")
logger.critical("This is a critical message")

def change_color_mode(self):
"""Change the color mode of the log widget."""
current_mode = self.log_widget.color_mode
new_mode = 'full' if current_mode == 'level' else 'level' if current_mode == 'message' else 'message'
self.log_widget.color_mode = new_mode

logger.info(f"Changed color mode to: {new_mode}")

def change_log_level(self):
"""Change the logging level of the log widget."""
current_level = self.log_widget.get_logging_level()
levels = ["TRACE", "DEBUG", "INFO", "SUCCESS", "WARNING", "ERROR", "CRITICAL"]
current_index = levels.index(current_level)
new_index = (current_index + 1) % len(levels)
new_level = levels[new_index]

self.log_widget.set_logging_level(new_level)
logger.log(new_level, f"Changed logging level from {current_level} to: {new_level}")

def main():
root = tk.Tk()
app = LoguruWidgetPackExample(root)
root.mainloop()

if __name__ == "__main__":
main()
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,15 @@ def create_widgets(self):
)
self.continuous_log_btn.pack(fill=X, pady=5)

# Add a button to change log level
self.change_level_btn = ttk.Button(
button_frame,
text="Change Log Level",
command=self.change_log_level,
style="secondary.Outline.TButton"
)
self.change_level_btn.pack(fill=X, pady=5)

def log_message(self, level):
log_func = getattr(logger, level)
log_func(f"This is a {level} message")
Expand All @@ -70,6 +79,23 @@ def continuous_log(self):
count += 1
time.sleep(0.5)

def change_log_level(self):
levels = ["TRACE", "DEBUG", "INFO", "SUCCESS", "WARNING", "ERROR", "CRITICAL"]
level_no_to_name = {5: "TRACE", 10: "DEBUG", 20: "INFO", 25: "SUCCESS", 30: "WARNING", 40: "ERROR", 50: "CRITICAL"}

current_level_no = logger._core.min_level
current_level = level_no_to_name.get(current_level_no, "INFO") # Default to INFO if level is not found

current_index = levels.index(current_level)
new_index = (current_index + 1) % len(levels)
new_level = levels[new_index]

self.log_widget.set_logging_level(new_level)

# Use the appropriate logging function based on the new level
log_func = getattr(logger, new_level.lower())
log_func(f"Changed logging level from {current_level} to: {new_level}")

if __name__ == "__main__":
app = TkLoguruDemo()
app.mainloop()
app.mainloop()
Loading

0 comments on commit 025d31d

Please sign in to comment.