custom widget builder for a toplevel object #201
Replies: 1 comment 1 reply
-
Hello Benjamin, thanks for trying pygubu.
This happens because Window is not a widget, it is a Tk object with his own interpreter in the background.
Here you are missing the ro_properties of the builder object. Just mark the 'themename' property as read only and it will be passed to the init function. See code example. Question, Are you using a plugin structure to wrap your widgets? If not, This is the code updated, using one file only: import tkinter as tk
import ttkbootstrap as ttkb
from pygubu.api.v1 import register_widget, register_custom_property
from pygubu.plugins.tk.tkstdwidgets import TKToplevel
class TtkbWindowBO(TKToplevel):
class_ = ttkb.Window
properties = TKToplevel.properties + ("themename", "alpha")
ro_properties = TKToplevel.ro_properties + ("themename", "alpha")
def realize(self, parent, extra_init_args: dict = None):
args = self._get_init_args(extra_init_args)
# master = parent.get_child_master()
self.widget = ttkb.Window(**args)
return self.widget
_bid = "ttk.Window"
register_widget(_bid, TtkbWindowBO, "Window", ("ttkbootstrap", "ttk"))
register_custom_property(_bid, "themename", "choice",
values=("default", "cosmo", "litera"),
state="readonly")
register_custom_property(_bid, "alpha", "realnumber", "1.0")
#
#
# Helper classes for Designer
#
#
from pygubu.plugins.pygubu.designer.basehelpers import (
ToplevelPreviewFactory,
ToplevelPreviewMixin,
ToplevelPreviewBaseBO,
)
from pygubu.api.v1 import IDesignerPlugin, IPluginBase
from pygubu.component.plugin_manager import PluginManager
# I will create a Preview of the ttb.Window using a Frame
# to avoid a extra window apearing in designer
# This preview will have Toplevel attributes so
# reuse the ToplevelPreviewMixin
WindowFramePreview = ToplevelPreviewFactory(
"WindowFramePreview",
(ToplevelPreviewMixin, ttkb.Frame, object),
{},
)
# Also we need a Builder for the preview.
# This will ignore some properties of window
# because we are using a fake Window.
class WindowFramePreviewBO(ToplevelPreviewBaseBO):
class_ = WindowFramePreview
def _set_property(self, target_widget, pname, value):
if pname == "alpha":
print(f"{pname} ignored for now")
elif pname == "themename":
print(f"Do something to emulate this ...")
else:
super()._set_property(target_widget, pname, value)
class MyDesignerPlugin(IDesignerPlugin, IPluginBase):
def do_activate(self) -> bool:
return True
def get_designer_plugin(self):
"""Return class instance that implements IDesignerPlugin"""
return self
def get_preview_builder(self, builder_uid: str):
"""Return a BuilderObject subclass used to build a preview
for the target builder_uid"""
if builder_uid == "ttk.Window":
return WindowFramePreviewBO
return None
def get_toplevel_preview_for(
self, builder_uid: str, widget_id: str, builder, top_master
):
"""Return a Toplevel with the target widget_id rendered inside."""
top = None
if builder_uid == "ttk.Window":
# Having two Tk windows will be problematic.
# Expect errors.
top = tk.Toplevel(top_master)
top.geometry("320x240")
txt = """Having two Tk windows will be problematic.
Sorry, no Toplevel preview available :("""
msg = ttkb.Label(top, text=txt)
msg.pack(expand=True)
return top
# Force plugin load as I'm using one file and not a plugin module
plugin = MyDesignerPlugin()
PluginManager.designer_plugins.append(plugin) Let me know if you have more questions. |
Beta Was this translation helpful? Give feedback.
-
hello!
i'm hoping to wrap some of the widgets included in ttkbootstrap, and am struggling namely with the Window class. it takes a 'themename' argument on initialization, and uses that to set colors for elements through their 'bootstyle' parameters.
i observed the inclusion of the CTk widget for customtkinter's plugin, and am trying to replicate some of it's logic, but i'm in a smidge over my head (lol)
so far, i have the builder loading without errors, but there are a couple of issues.
here's my code so far:
my assumption is that i need to redefine at least one more function from TKToplevel, but my head is starting to spin about it and i figured asking for help made sense. i've been doing this sort of exploring for a few days now lol
thanks in advance for any advice! (also: i did successfully implement ttkbbootstrap's Treeview with a bootstyle field earlier, so that can be done!)
Beta Was this translation helpful? Give feedback.
All reactions