Replies: 4 comments 2 replies
-
Hello Mark,
I see your problem here. I think that the best solution will be to allow pygubu designer to fully import the styles file as module, and not to execute its content as it is doing now.
I think in some modifications to the designer. One restriction is that the styles should be created after the root window is instantiated and before any child widget is created. (alejandroautalan/pygubu#282) Continuing with this idea, the module will need to define the styles inside a method and not at module level. For example, a method named "setup_ttk_styles". Remove definition file from global configuration and move them to project level properties:
The following example shows how it would look in the future. Definition file example custom_styles_module.py: import tkinter as tk
import tkinter.ttk as ttk
def setup_ttk_styles(master=None):
style = ttk.Style()
style.configure("MySpecialButton.TButton",
font=("helvetica", 12, "bold"),
background="green", foreground="white") Code generated in Application Mode: #!/usr/bin/python3
import pathlib
import tkinter.ttk as ttk
import pygubu
import custom_styles_module
PROJECT_PATH = pathlib.Path(__file__).parent
PROJECT_UI = PROJECT_PATH / "helloapp.ui"
class HelloappApp:
def __init__(self, master=None):
self.builder = builder = pygubu.Builder(
on_first_object=custom_styles_module.setup_ttk_styles)
builder.add_resource_path(PROJECT_PATH)
builder.add_from_file(PROJECT_UI)
# Main widget
self.mainwindow = builder.get_object("toplevel1", master)
builder.connect_callbacks(self)
def run(self):
self.mainwindow.mainloop()
if __name__ == "__main__":
app = HelloappApp()
app.run() Code generated in Script Mode: #!/usr/bin/python3
import tkinter as tk
import tkinter.ttk as ttk
import custom_styles_module
class HelloappApp:
def __init__(self, master=None):
# build ui
toplevel1 = tk.Tk() if master is None else tk.Toplevel(master)
toplevel1.configure(height=200, width=200)
toplevel1.geometry("320x100")
# First object created
custom_styles_module.setup_ttk_styles(toplevel1)
frame1 = ttk.Frame(toplevel1)
frame1.configure(height=200, padding=5, width=200)
frame2 = ttk.Frame(frame1)
frame2.configure(height=200, width=200)
label1 = ttk.Label(frame2)
label1.configure(text='Hello world!')
label1.pack(side="top")
frame2.pack(expand="true", side="top")
button1 = ttk.Button(frame1)
button1.configure(text='quit')
button1.pack(anchor="e", side="top")
frame1.pack(expand="true", fill="both", side="top")
# Main widget
self.mainwindow = toplevel1
def run(self):
self.mainwindow.mainloop()
if __name__ == "__main__":
app = HelloappApp()
app.run() Let me know what do you think of this approach. Regards |
Beta Was this translation helpful? Give feedback.
-
Alejandro, I am trying to get this to work. I created a styles python file as you suggested. and had the function called right after creating root. But now when I point pygubu-designer to this new format file with the function def in the preferences, it does not pickup the styles. I am sure i am doing something obviously wrong... Regards mark |
Beta Was this translation helpful? Give feedback.
-
No worries and no rush. I am actually trying to get a release candidate 1 out, and if I really should just focus on that! This gave me an excuse that I should not have taken to procrastinate. Regards, Mark |
Beta Was this translation helpful? Give feedback.
-
Fixed in pygubu-designer v0.37 |
Beta Was this translation helpful? Give feedback.
-
Hi,
Wondering if there is a better way of using the style definitions file.
My application is made of up of perhaps a dozen high level widgets generated by pygubu designer. And I use styles everywhere This means that the style code gets generated and included in each widget. This opens the door for these copies to get out of synch where I add/change styles for one widget and forget to regenerate all the other widgets with the changed style file.
Any suggestions here? I could imagine the style file being generated once and then imported to any widget that uses it. But don't see any place to inject that statement. And even if I did, pygubu designer still needs access to the styles to generate the use of a style.
Thanks!
Mark
Beta Was this translation helpful? Give feedback.
All reactions