-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
657 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
import warnings | ||
warnings.filterwarnings("ignore", category=SyntaxWarning, module="ttkbootstrap.localization.msgs") | ||
import ttkbootstrap as ttk | ||
from ttkbootstrap.constants import * | ||
from tkloguru import LoguruWidget, setup_logger | ||
from loguru import logger | ||
import threading | ||
import time | ||
|
||
class TkLoguruDemo(ttk.Window): | ||
def __init__(self): | ||
super().__init__(themename="darkly") | ||
self.title("TkLoguru with ttkbootstrap Demo") | ||
self.geometry("800x600") | ||
|
||
self.create_widgets() | ||
setup_logger(self.log_widget) | ||
|
||
def create_widgets(self): | ||
main_frame = ttk.Frame(self) | ||
main_frame.pack(fill=BOTH, expand=YES, padx=10, pady=10) | ||
|
||
# Create the LoguruWidget | ||
self.log_widget = LoguruWidget(main_frame, show_scrollbar=True, color_mode='level', max_lines=1000) | ||
self.log_widget.pack(side=LEFT, fill=BOTH, expand=YES) | ||
|
||
# Create a frame for buttons | ||
button_frame = ttk.Frame(main_frame) | ||
button_frame.pack(side=RIGHT, fill=Y, padx=(10, 0)) | ||
|
||
# Add buttons for different log levels | ||
levels = ['debug', 'info', 'success', 'warning', 'error', 'critical'] | ||
for level in levels: | ||
btn = ttk.Button( | ||
button_frame, | ||
text=level.capitalize(), | ||
command=lambda l=level: self.log_message(l), | ||
style=f"{level.upper()}.TButton" | ||
) | ||
btn.pack(fill=X, pady=5) | ||
|
||
# Add a button to start continuous logging | ||
self.continuous_logging = False | ||
self.continuous_log_btn = ttk.Button( | ||
button_frame, | ||
text="Start Continuous Log", | ||
command=self.toggle_continuous_log, | ||
style="info.Outline.TButton" | ||
) | ||
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") | ||
|
||
def toggle_continuous_log(self): | ||
self.continuous_logging = not self.continuous_logging | ||
if self.continuous_logging: | ||
self.continuous_log_btn.config(text="Stop Continuous Log") | ||
threading.Thread(target=self.continuous_log, daemon=True).start() | ||
else: | ||
self.continuous_log_btn.config(text="Start Continuous Log") | ||
|
||
def continuous_log(self): | ||
levels = ['debug', 'info', 'success', 'warning', 'error', 'critical'] | ||
count = 0 | ||
while self.continuous_logging: | ||
level = levels[count % len(levels)] | ||
self.log_message(level) | ||
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() |
Oops, something went wrong.