Replies: 2 comments
-
Hello @Mike-152 thanks for trying pygubu.
The ttk.Entry widget does not have a text property. The designer adds a special "text" property to simplify UI creation. To modify text of Entry, use the methods insert, delete and get.
Or use a textvariable.
I modified the UI and code to use a textvariable.
This is more complex, but it can be done. See the code bellow. #!/usr/bin/python3
import pathlib
import pygubu
PROJECT_PATH = pathlib.Path(__file__).parent
PROJECT_UI = PROJECT_PATH / "pygubu-test.ui"
class PygubuTestApp:
def __init__(self, master=None):
self.builder = builder = pygubu.Builder()
builder.add_resource_path(PROJECT_PATH)
builder.add_from_file(PROJECT_UI)
# Main widget
self.mainwindow = builder.get_object("toplevel1", master)
self.question1_var = None
builder.import_variables(self, ['question1_var'])
# Main menu
_main_menu = builder.get_object("menu1", self.mainwindow)
self.mainwindow.configure(menu=_main_menu)
builder.connect_callbacks(self)
#
# App init
title = "-- New window --"
dft_answer = "please answer"
sub_menu_status = False
# change label1
self.x = builder.get_object("label1")
self.x.configure(text=title)
# replace default answer on entry1
self.y = builder.get_object("entry1")
self.question1_var.set(dft_answer)
#
# make mnu_choice1 inactive
# ??
new_state = "normal" if sub_menu_status is True else "disabled"
file_menu = _main_menu.nametowidget(
_main_menu.entrycget("File", "menu"))
file_menu.entryconfigure("Click here", state=new_state)
self.file_menu = file_menu
def menu_click(self):
print("Call anything")
def run(self):
self.mainwindow.mainloop()
if __name__ == "__main__":
app = PygubuTestApp()
app.run() <?xml version='1.0' encoding='utf-8'?>
<interface version="1.3">
<object class="tk.Toplevel" id="toplevel1">
<property name="height">200</property>
<property name="width">200</property>
<child>
<object class="ttk.Frame" id="frame1">
<property name="height">200</property>
<property name="width">200</property>
<layout manager="pack">
<property name="side">top</property>
</layout>
<child>
<object class="ttk.Label" id="label1">
<property name="state">normal</property>
<property name="text" translatable="yes">Default text</property>
<layout manager="grid">
<property name="column">0</property>
<property name="columnspan">2</property>
<property name="row">0</property>
</layout>
</object>
</child>
<child>
<object class="ttk.Label" id="label2">
<property name="text" translatable="yes">Question 1 :</property>
<layout manager="grid">
<property name="column">0</property>
<property name="row">1</property>
</layout>
</object>
</child>
<child>
<object class="ttk.Entry" id="entry1">
<property name="text" translatable="yes">your answer here</property>
<property name="textvariable">string:question1_var</property>
<layout manager="grid">
<property name="column">1</property>
<property name="row">1</property>
<property name="sticky">w</property>
</layout>
</object>
</child>
</object>
</child>
</object>
<object class="tk.Menu" id="menu1">
<child>
<object class="tk.Menuitem.Submenu" id="submenu1">
<property name="label" translatable="yes">File</property>
<child>
<object class="tk.Menuitem.Command" id="mnu_choice1" named="True">
<property name="command" type="command" cbtype="simple">menu_click</property>
<property name="label" translatable="yes">Click here</property>
</object>
</child>
</object>
</child>
</object>
</interface> Regards |
Beta Was this translation helpful? Give feedback.
-
Thank you very much Alejandro, this is fine ! |
Beta Was this translation helpful? Give feedback.
-
I'm new in both python and pygubu...
I have made a simple window with a label, entry and a menu with submenu and I have the following question/problem :
(see code)
Thanks a lot for your attention
python code
ui file
Beta Was this translation helpful? Give feedback.
All reactions