### Install All Official Extensions Source: https://github.com/xiaokang2022/maliang/blob/main/README.zh-Hans.md Install all official extension packages for specific functionalities, such as maliang-mpl, maliang-media, maliang-three, and maliang-table. ```shell pip install maliang[ext] ``` -------------------------------- ### Example Blog Post Structure Source: https://github.com/xiaokang2022/maliang/blob/main/docs/blog/posts/official/1.md A complete example demonstrating the structure of a user-submitted blog post, including metadata, title, overview, and content. ```markdown --- comments: true date: created: 2333-02-23 updated: 6666-06-06 authors: - 我叫用户名 categories: - 用户投稿 tags: - 标签一 - 标签二 - 标签三 --- # 我是文章标题 我是文章概览 啊吧啊吧…… ``` -------------------------------- ### Install All Optional Packages Source: https://github.com/xiaokang2022/maliang/blob/main/README.zh-Hans.md Install all optional packages to enable additional features. This command includes dependencies like darkdetect, pillow, pywinstyles, hPyT, and win32material. ```shell pip install maliang[opt] ``` -------------------------------- ### Install Maliang Package Source: https://github.com/xiaokang2022/maliang/blob/main/README.zh-Hans.md Use this command to install the base Maliang package. Ensure your Python version is 3.10 or higher. ```shell pip install maliang ``` -------------------------------- ### Blog Post Metadata Configuration Source: https://github.com/xiaokang2022/maliang/blob/main/docs/blog/posts/official/1.md Example of the front matter metadata required at the beginning of each Markdown blog post file for the blog plugin to recognize it. ```markdown --- comments: true date: created: 9999-09-09 updated: 2024-11-11 authors: - Xiaokang2022 categories: - 官方文章 tags: - 网站 - 新增功能 - 说明 --- ``` -------------------------------- ### Theme Manager for Color Mode and Events Source: https://context7.com/xiaokang2022/maliang/llms.txt Shows how to get the current color mode, force switch to dark mode, register callbacks for theme changes, and revert to system settings. Also demonstrates removing event listeners. ```python import maliang from maliang.theme import manager root = maliang.Tk(size=(600, 400), title="主题管理示例") cv = maliang.Canvas(root) cv.place(width=600, height=400) # 获取当前颜色模式 print(manager.get_color_mode()) # "light" 或 "dark" # 强制切换为暗色模式 manager.set_color_mode("dark") # 注册主题变更回调 def on_theme_change(theme: str): print(f"主题切换为:{theme}") manager.register_event(on_theme_change) # 还原跟随系统 manager.set_color_mode("system") # 不再需要时移除 manager.remove_event(on_theme_change) root.mainloop() ``` -------------------------------- ### Get Screen Dimensions Source: https://context7.com/xiaokang2022/maliang/llms.txt Retrieves the width and height of the primary screen. This is useful for positioning windows or elements. ```python w, h = utility.screen_size() print(f"屏幕:{w}x{h}") ``` -------------------------------- ### Create CheckBox Widgets Source: https://context7.com/xiaokang2022/maliang/llms.txt The CheckBox widget allows users to select multiple options. Create individual CheckBox instances and use `get()` to determine if each is selected. A common pattern is to group them with their associated labels. ```python import maliang root = maliang.Tk(size=(600, 400), title="CheckBox Example") cv = maliang.Canvas(root) cv.place(width=600, height=400) options = ["Python", "JavaScript", "Rust"] checks = [] for i, opt in enumerate(options): cb = maliang.CheckBox(cv, (100, 80 + i*50), length=28, default=False) maliang.Text(cv, (140, 80 + i*50), text=opt) checks.append(cb) def get_selected(): selected = [opt for cb, opt in zip(checks, options) if cb.get()] print("Selected:", selected) maliang.Button(cv, (200, 260), text="Get Options", command=get_selected) root.mainloop() ``` -------------------------------- ### Create InputBox for Text Entry Source: https://context7.com/xiaokang2022/maliang/llms.txt The InputBox widget provides single-line text input. It supports placeholders, character masking (for passwords using `show='*'`), and character limits. Use `get()` to retrieve the input text and `clear()` to reset it. ```python import maliang root = maliang.Tk(size=(600, 400), title="InputBox Example") cv = maliang.Canvas(root) cv.place(width=600, height=400) # Normal input box name_input = maliang.InputBox(cv, (100, 100), size=(300, 40), placeholder="Enter username") # Password box pwd_input = maliang.InputBox(cv, (100, 160), size=(300, 40), placeholder="Enter password", show="*") def submit(): print("Username:", name_input.get()) print("Password:", pwd_input.get()) name_input.clear() maliang.Button(cv, (200, 230), text="Submit", command=submit) root.mainloop() ``` -------------------------------- ### Create Semi-Transparent Smoke Image Source: https://context7.com/xiaokang2022/maliang/llms.txt Generates a semi-transparent image, often used as a mask or overlay. Requires the Pillow library to be installed. The color parameter can include alpha information. ```python smoke = utility.create_smoke((600, 400), color="#00000088") maliang.Image(cv, (0, 0), size=(600, 400), image=smoke) ``` -------------------------------- ### Display and Update Text Source: https://context7.com/xiaokang2022/maliang/llms.txt The Text widget displays plain text. It automatically calculates its size based on content. Text can be dynamically updated using `set()` and retrieved using `get()`. ```python import maliang root = maliang.Tk(size=(600, 400), title="Text Example") cv = maliang.Canvas(root) cv.place(width=600, height=400) # Auto-calculate size t = maliang.Text(cv, (50, 50), text="Hello, maliang!", fontsize=20, weight="bold") # Dynamically change text t.set("Text updated") print(t.get()) # "Text updated" root.mainloop() ``` -------------------------------- ### Create and Configure Tk Main Window Source: https://context7.com/xiaokang2022/maliang/llms.txt Use Tk to create the main application window. It automatically binds the theme manager and supports window scaling. Configure properties like size, title, centering, transparency, and topmost behavior. The `at_exit` callback is executed when the window is closed. ```python import maliang # Create main window, size 1280x720, title "Demo", centered root = maliang.Tk(size=(1280, 720), title="Demo") root.center() # Center on screen # Set transparency root.alpha(0.95) # Keep on top root.topmost(True) # Callback on exit root.at_exit(lambda: print("Window closed")) root.mainloop() ``` -------------------------------- ### Load and Scale PhotoImage Source: https://context7.com/xiaokang2022/maliang/llms.txt Demonstrates loading an image using Maliang's PhotoImage class and performing scaling operations. Supports scaling by a factor or to a specific pixel size. ```python img = maliang.PhotoImage(file="photo.png") img_half = img.scale(0.5, 0.5) img_resized = img.resize(200, 150) maliang.Image(cv, (50, 50), image=img_resized) ``` -------------------------------- ### Maliang Animation Controllers Source: https://context7.com/xiaokang2022/maliang/llms.txt Demonstrates the usage of built-in easing functions (linear, smooth, rebound, ease_in, ease_out) and how to generate custom controllers using mathematical functions. ```python from maliang.animation import controllers import math # 内置控制器 print(controllers.linear(0.5)) # 0.5 print(controllers.smooth(0.5)) # ~0.5(cos 曲线) print(controllers.rebound(0.9)) # > 1(回弹超出) print(controllers.ease_in(0.5)) # < 0.5(先慢后快) print(controllers.ease_out(0.5)) # > 0.5(先快后慢) # 自定义:用 sin 函数生成控制器(范围 0→π/2) my_ctrl = controllers.generate(math.sin, 0, math.pi / 2) print(my_ctrl(1.0)) # 1.0 ``` -------------------------------- ### Custom Window Appearance with Mica and Pywinstyles Source: https://context7.com/xiaokang2022/maliang/llms.txt Applies custom window appearances, including Mica material effect (requires win32material) and custom border/header colors (requires pywinstyles). This functionality is Windows-specific. ```python import maliang from maliang.theme import manager root = maliang.Tk(size=(800, 500), title="自定义窗口") # 应用 Mica 材质效果(需要 win32material) manager.apply_theme(root, theme="mica") # 自定义标题栏颜色(需要 pywinstyles) manager.customize_window( root, border_color="#4A90D9", header_color="#1A1A2E", title_color="#FFFFFF", border_type="round", ) cv = maliang.Canvas(root) cv.place(width=800, height=500) maliang.Label(cv, (300, 220), text="自定义窗口") root.mainloop() ``` -------------------------------- ### Create and Configure Button Source: https://context7.com/xiaokang2022/maliang/llms.txt The Button widget triggers a callback function when clicked. It supports displaying text and optionally an icon. The `command` parameter specifies the function to execute on click. ```python import maliang root = maliang.Tk(size=(600, 400), title="Button Example") cv = maliang.Canvas(root) cv.place(width=600, height=400) count = [0] def on_click(): count[0] += 1 btn.set(f"Clicked {count[0]} times") btn = maliang.Button(cv, (200, 180), text="Click Me", fontsize=16, command=on_click) root.mainloop() ``` -------------------------------- ### Custom Animation with GradientItem Source: https://context7.com/xiaokang2022/maliang/llms.txt Demonstrates creating a custom animation to change a label's background color using GradientItem. Requires importing maliang and specific animation modules. ```python import maliang from maliang.animation import animations, controllers root = maliang.Tk(size=(600, 400), title="Animation 示例") cv = maliang.Canvas(root) cv.place(width=600, height=400) box = maliang.Label(cv, (50, 180), size=(100, 40), text="移动") # 自定义动画:让标签的背景色从白色过渡到蓝色(通过 GradientItem 实现) anim = animations.GradientItem( cv, box.shapes[0].items[0], "fill", ("#FFFFFF", "#4A90D9"), duration=1000, controller=controllers.smooth, fps=60, ) anim.start() root.mainloop() ``` -------------------------------- ### Create RadioBox Widgets in a Group Source: https://context7.com/xiaokang2022/maliang/llms.txt The RadioBox widget allows selection of a single option from a group. Use the `group()` method to associate multiple RadioBox instances, making them mutually exclusive. `set(True)` selects a radio button, and `get()` returns its state. ```python import maliang root = maliang.Tk(size=(600, 400), title="RadioBox Example") cv = maliang.Canvas(root) cv.place(width=600, height=400) themes = ["Light Theme", "Dark Theme", "System Default"] radios = [] for i, theme in enumerate(themes): rb = maliang.RadioBox(cv, (100, 80 + i*50), length=28) maliang.Text(cv, (140, 80 + i*50), text=theme) radios.append(rb) # Group the radio buttons (mutually exclusive) radios[0].group(*radios[1:]) radios[0].set(True) # Default selection def apply(): for i, rb in enumerate(radios): if rb.get(): print(f"Selected: {themes[i]}") maliang.Button(cv, (200, 260), text="Apply", command=apply) root.mainloop() ``` -------------------------------- ### Create Toplevel Dialog Window Source: https://context7.com/xiaokang2022/maliang/llms.txt Use Toplevel to create child windows, which can be modal (grab focus). The `grab=True` argument makes the dialog modal. Use `dialog.destroy` to close the Toplevel window. ```python import maliang root = maliang.Tk(size=(800, 600), title="Main Window") def open_dialog(): dialog = maliang.Toplevel(root, size=(400, 300), title="Dialog", grab=True) canvas = maliang.Canvas(dialog) canvas.place(width=400, height=300) maliang.Button(canvas, (150, 130), text="Close", command=dialog.destroy) canvas = maliang.Canvas(root) canvas.place(width=800, height=600) maliang.Button(canvas, (350, 280), text="Open Dialog", command=open_dialog) root.mainloop() ``` -------------------------------- ### ToggleButton - Create and use a toggle button Source: https://context7.com/xiaokang2022/maliang/llms.txt Use ToggleButton for on/off state switching. The command callback receives the boolean state. ```python import maliang root = maliang.Tk(size=(600, 400), title="ToggleButton 示例") cv = maliang.Canvas(root) cv.place(width=600, height=400) def on_toggle(value: bool): print("粗体:", value) tb = maliang.ToggleButton(cv, (220, 180), text="粗体 B", default=False, command=on_toggle) print(tb.get()) # False root.mainloop() ``` -------------------------------- ### Window Movement Animation with MoveWindow Source: https://context7.com/xiaokang2022/maliang/llms.txt Applies animation to the window itself, such as a shaking effect. The animation is triggered by a button click. ```python import maliang from maliang.animation import animations, controllers root = maliang.Tk(size=(600, 400), title="窗口动画示例", position=(100, 100)) def shake(): anim = animations.MoveWindow(root, (200, 0), 400, controller=controllers.rebound, fps=60) anim.start() cv = maliang.Canvas(root) cv.place(width=600, height=400) maliang.Button(cv, (250, 180), text="抖动窗口", command=shake) root.mainloop() ``` -------------------------------- ### Blog Post Directory Structure Source: https://github.com/xiaokang2022/maliang/blob/main/docs/blog/posts/official/1.md Specifies the directory structure for official and user-submitted blog posts within the documentation source. ```tree └─docs └─blog └─posts ├─official # (1)! └─users ``` -------------------------------- ### Create and Control Switch Widget Source: https://context7.com/xiaokang2022/maliang/llms.txt The Switch widget is a sliding toggle with smooth animation. The `command` callback receives a boolean indicating the new state. Use `set()` to programmatically change the state, with an optional `callback` argument to trigger the command. ```python import maliang root = maliang.Tk(size=(600, 400), title="Switch Example") cv = maliang.Canvas(root) cv.place(width=600, height=400) status_text = maliang.Text(cv, (150, 100), text="Status: Off") def on_toggle(value: bool): status_text.set(f"Status: {'On' if value else 'Off'}") sw = maliang.Switch(cv, (260, 190), length=70, default=False, command=on_toggle) # Programmatic setting (no callback) sw.set(True) # Programmatic setting with callback sw.set(False, callback=True) root.mainloop() ``` -------------------------------- ### IconButton - Create a button with an icon Source: https://context7.com/xiaokang2022/maliang/llms.txt IconButton displays text alongside an icon. Ensure the image file path is correct. ```python import maliang root = maliang.Tk(size=(600, 400), title="IconButton 示例") cv = maliang.Canvas(root) cv.place(width=600, height=400) img = maliang.PhotoImage(file="icon.png") # 替换为实际图片路径 ib = maliang.IconButton(cv, (200, 180), text="上传文件", image=img, command=lambda: print("上传")) root.mainloop() ``` -------------------------------- ### OptionButton - Create a dropdown selection menu Source: https://context7.com/xiaokang2022/maliang/llms.txt OptionButton displays a list of options that appear when clicked, allowing single selection. The 'align' parameter controls dropdown direction. ```python import maliang root = maliang.Tk(size=(600, 400), title="OptionButton 示例") cv = maliang.Canvas(root) cv.place(width=600, height=400) def on_select(index): print("选择了:", ["苹果", "香蕉", "樱桃"][index]) opt = maliang.OptionButton( cv, (200, 180), size=(200, 36), text=("苹果", "香蕉", "樱桃"), command=on_select, align="down", ) root.mainloop() ``` -------------------------------- ### User Blog Post Naming Convention Source: https://github.com/xiaokang2022/maliang/blob/main/docs/blog/posts/official/1.md Defines the required naming convention for user-submitted blog post files. ```text YYYYMMDD-GitHub用户名.md ``` -------------------------------- ### Spinner - Create determinate and indeterminate spinners Source: https://context7.com/xiaokang2022/maliang/llms.txt Spinner widgets can show progress (determinate) or indicate ongoing activity (indeterminate). ```python import maliang root = maliang.Tk(size=(600, 400), title="Spinner 示例") cv = maliang.Canvas(root) cv.place(width=600, height=400) # 不确定模式(持续旋转) sp_indet = maliang.Spinner(cv, (260, 150), size=(80, 80), mode="indeterminate") # 确定模式(显示进度) sp_det = maliang.Spinner(cv, (260, 260), size=(80, 80), mode="determinate", default=0.65) root.mainloop() ``` -------------------------------- ### Image - Display and update images Source: https://context7.com/xiaokang2022/maliang/llms.txt The Image widget displays static images and supports updating the displayed image. Ensure image file paths are correct. ```python import maliang root = maliang.Tk(size=(600, 400), title="Image 示例") cv = maliang.Canvas(root) cv.place(width=600, height=400) photo = maliang.PhotoImage(file="logo.png") # 替换为实际图片 img_widget = maliang.Image(cv, (200, 100), size=(200, 150), image=photo) # 替换图片 new_photo = maliang.PhotoImage(file="new_logo.png") img_widget.set(new_photo) root.mainloop() ``` -------------------------------- ### Sponsor Information Format Source: https://github.com/xiaokang2022/maliang/blob/main/docs/Sponsor.md Use this format when making a donation to specify your nickname or GitHub username. This information will be displayed alongside your donation details. ```text maliang [你的昵称或者你的 GitHub 用户名] ``` -------------------------------- ### Create Maliang Canvas Container Source: https://context7.com/xiaokang2022/maliang/llms.txt The Canvas widget serves as the parent container for all virtual controls. Set `auto_zoom=True` to enable automatic scaling of controls when the window is resized. `expand="xy"` makes the canvas fill the available space. ```python import maliang root = maliang.Tk(size=(800, 600), title="Canvas Example") # auto_zoom=True means controls scale with window resizing canvas = maliang.Canvas(root, auto_zoom=True, expand="xy") canvas.place(width=800, height=600) maliang.Label(canvas, (50, 50), text="Auto-scaling Label") root.mainloop() ``` -------------------------------- ### RGB Color Operations: Contrast and Transition Source: https://context7.com/xiaokang2022/maliang/llms.txt Provides functions for color manipulation, including calculating the contrast color and finding the intermediate color between two given RGB values. ```python from maliang.color import rgb, convert # 获取对比色 c = rgb.contrast((100, 150, 200)) print(c) # (155, 105, 55) # 两色过渡(rate=0.5 为中间色) mid = rgb.transition((255, 0, 0), (0, 0, 255), 0.5) print(mid) # (128, 0, 128) ``` -------------------------------- ### ProgressBar - Animate progress from 0 to 100% Source: https://context7.com/xiaokang2022/maliang/llms.txt Use ProgressBar to visually represent task completion. Animations can be used to smoothly update the progress value. ```python import maliang from maliang.animation import animations, controllers root = maliang.Tk(size=(600, 400), title="ProgressBar 示例") cv = maliang.Canvas(root) cv.place(width=600, height=400) pb = maliang.ProgressBar(cv, (100, 180), size=(400, 24), default=0) # 用动画将进度条从 0 平滑推进到 100% anim = animations.Animation( 2000, lambda p: pb.set(p), controller=controllers.smooth, fps=60, ) anim.start() root.mainloop() ``` -------------------------------- ### Create a Label Widget Source: https://context7.com/xiaokang2022/maliang/llms.txt The Label widget displays information with a background. It supports rounded corners on modern OS (Windows 11/macOS/Linux) or rectangular backgrounds on older systems (Windows 10). ```python import maliang root = maliang.Tk(size=(600, 400), title="Label Example") cv = maliang.Canvas(root) cv.place(width=600, height=400) label = maliang.Label(cv, (50, 50), text="Version: 3.1.5", fontsize=14) root.mainloop() ``` -------------------------------- ### Generate Gradient Color List Source: https://context7.com/xiaokang2022/maliang/llms.txt Creates a list of colors representing a gradient between two specified RGB colors. The `smooth` controller is used for interpolation. ```python from maliang.animation import controllers gradient = rgb.gradient((255, 0, 0), (0, 0, 255), 10, controller=controllers.smooth) print(gradient[5]) # 中间色 ``` -------------------------------- ### UnderlineButton - Create a hyperlink-style button Source: https://context7.com/xiaokang2022/maliang/llms.txt UnderlineButton mimics the appearance of a hyperlink and can be used to trigger actions, such as opening a URL. ```python import maliang import webbrowser root = maliang.Tk(size=(600, 400), title="UnderlineButton 示例") cv = maliang.Canvas(root) cv.place(width=600, height=400) maliang.UnderlineButton( cv, (220, 190), text="访问 GitHub 主页", command=lambda: webbrowser.open("https://github.com/Xiaokang2022/maliang"), ) root.mainloop() ``` -------------------------------- ### Widget Movement Animation with MoveWidget Source: https://context7.com/xiaokang2022/maliang/llms.txt Animates a widget's position using MoveWidget. Features a rebound effect and can be set to repeat. The animation is triggered by a button click. ```python import maliang from maliang.animation import animations, controllers root = maliang.Tk(size=(600, 400), title="MoveWidget 示例") cv = maliang.Canvas(root) cv.place(width=600, height=400) btn = maliang.Button(cv, (50, 180), text="弹跳", command=lambda: move_anim.start()) move_anim = animations.MoveWidget( btn, offset=(300, 0), # 向右移动 300px duration=800, controller=controllers.rebound, # 回弹效果 fps=60, repeat=1, # 来回一次(repeat=1 表示再重复 1 次) ) root.mainloop() ``` -------------------------------- ### Slider - Create a slider with a value display Source: https://context7.com/xiaokang2022/maliang/llms.txt Sliders allow users to select a value within a range. A callback function can update a text display with the current value. ```python import maliang root = maliang.Tk(size=(600, 400), title="Slider 示例") cv = maliang.Canvas(root) cv.place(width=600, height=400) val_text = maliang.Text(cv, (270, 120), text="0.00") def on_slide(value: float): val_text.set(f"{value:.2f}") slider = maliang.Slider(cv, (100, 180), size=(400, 30), default=0, command=on_slide) root.mainloop() ``` -------------------------------- ### Tooltip - Add hover tooltips to widgets Source: https://context7.com/xiaokang2022/maliang/llms.txt Tooltips provide contextual information when a user hovers over a widget. The 'align' parameter controls tooltip position relative to the widget. ```python import maliang root = maliang.Tk(size=(600, 400), title="Tooltip 示例") cv = maliang.Canvas(root) cv.place(width=600, height=400) btn = maliang.Button(cv, (220, 180), text="保存", command=lambda: print("保存")) maliang.Tooltip(btn, text="保存当前文件 (Ctrl+S)", align="down") root.mainloop() ``` -------------------------------- ### SpinBox - Create a numeric input with steppers Source: https://context7.com/xiaokang2022/maliang/llms.txt SpinBox is a numeric input field with up/down arrows for incremental changes. It supports custom formatting and step values. ```python import maliang root = maliang.Tk(size=(600, 400), title="SpinBox 示例") cv = maliang.Canvas(root) cv.place(width=600, height=400) spin = maliang.SpinBox( cv, (200, 180), size=(200, 40), format_spec=".1f", # 保留一位小数 step=0.5, # 每次变化 0.5 default="1.0", ) maliang.Button(cv, (250, 260), text="获取值", command=lambda: print(spin.get())) root.mainloop() ``` -------------------------------- ### Load Custom Font Source: https://context7.com/xiaokang2022/maliang/llms.txt Attempts to load a custom font file. This function is primarily for Windows and Linux systems. Returns a boolean indicating success or failure. ```python success = utility.load_font("my_font.ttf", private=True) print("字体加载:", "成功" if success else "失败") ``` -------------------------------- ### Blog Post Content Separator Source: https://github.com/xiaokang2022/maliang/blob/main/docs/blog/posts/official/1.md Indicates the marker used to separate the article's overview from its main content for display on the homepage. ```markdown ``` -------------------------------- ### Font Size Scaling Animation Source: https://context7.com/xiaokang2022/maliang/llms.txt Animates the font size of a label. The animation is triggered by a button click. ```python import maliang from maliang.animation import animations, controllers root = maliang.Tk(size=(600, 400), title="ScaleFontSize 示例") cv = maliang.Canvas(root) cv.place(width=600, height=400) label = maliang.Label(cv, (200, 180), text="缩放字体") # 字体从当前大小动画到 24px anim = animations.ScaleFontSize( label.texts[0], sizes=24, duration=600, controller=controllers.smooth, fps=60, ) maliang.Button(cv, (230, 260), text="触发", command=anim.start) root.mainloop() ```