How to save (and restore) the values of the graphical elements of the wxWidgets in Python?
I want some more friendly way, maybe using a for to scrape all the elements and save the current value in a txt when I close the window and restore when I load the app. I do not want to typing 2 line of code (save and restore) to each new element that I add.
#Igor, thanks by the tip of use config_base. I created this code based on:
configHandle = wx.Config(CONFIG_FILE)
# Sweep all elements in `self()` to find the grafical ones
# instance of the wxPython and salve the specific configuration.
for wxElement_name, wxElement_handle in self.__dict__.items():
# Each wxPython object have a specific parameter value
# to be saved and restored in the software initialization.
if isinstance(wxElement_handle, wx._core.TextCtrl):
configHandle.Write(wxElement_name, wxElement_handle.GetValue() )
elif isinstance(wxElement_handle, wx._core.CheckBox):
configHandle.Write(wxElement_name, ('True' if wxElement_handle.GetValue() else 'False') )
elif isinstance(wxElement_handle, wx._core.SpinCtrl):
configHandle.Write(wxElement_name, str(wxElement_handle.GetValue()) )
.
.
.
To restore I use the reverse logic of this code.
Related
Ok, so I get two errors whenever I try to run this script: but before I get ahead of myself: lets get to my objective.
create two joint chains, names are unimportant: but essentially I know that you can use brackets to list and isolate joint chains with matching children names. Instead my script seems to be ignoring the brackets and giving me the error anyways. I've tried every different flag for list relatives: but all that seems to do is change the error to something else.
I know that if this script was properly working it would only work on one joint chain because of the hardcoded names: but the script I'm pulling it from has name prefexes tied to the GUI to avoid hardcoding and allow adaptive naming: I'm only using the hardcoded as an example for this script. My complaint is this script doesn't work on ANY joint chain because I keep getting the error "more than one object matches name."
To run the script,save the following code as a .py in your maya documents script folder, restart your copy of maya, then open a new python tab and run the first three lines of code above import maya.cmds
'''
import exampleScriptTemplate
reload (exampleScriptTemplate)
exampleScriptTemplate.gui()
'''
import maya.cmds as cmds
if cmds.window("buildWin", exists =True):
cmds.deleteUI("buildWin", window = True)
myWindow = cmds.window("buildWin",t='DS_pvFinder',rtf=1,w=100, h=100, toolbox=True)
column = cmds.columnLayout(adj=True)
def gui(*args):
cmds.columnLayout()
cmds.button(w=300,label='build placement curve',c=printMultiple)
cmds.showWindow(myWindow)
def printMultiple(*args):
root = cmds.ls(sl=True)[0]
child = cmds.listRelatives(root,ad=1,f=True,children=True,type='joint')
child.append(root)
child.reverse()
limbJnt = child
print (child)
armroot = []
for j in limbJnt:
wstJnt = cmds.rename(child[3], 'wrist_BIND')
elbJnt = cmds.rename(child[2], 'elbow_BIND')
sdrJnt = cmds.rename(child[1], 'shoulder_BIND')
clvJnt = cmds.rename(child[0], 'clavicle_BIND')
armroot.append(j)
return armroot
I know I'm in the right ballpark. I just need to know how to properly use the brackets to store the list of what i'm selecting instead of searching all of worldspace and breaking.
Thank you for your help
The code you provided is incomplete, no window is opening, so I tried only the printMultiple function which causes a Error: No object matches name in my case.
Your code cannot work like this since you mix hardcoded names with a loop which does nothing. I suppose your main problem is the order of your renamings. The child array contains absolute names like:
[u'joint1', u'|joint1|joint2', u'|joint1|joint2|joint3']
If you now rename child[0] to 'clavicle_BIND', all the remaining elements in the list become invalid because their real names in the scene now look like this:
[u'clavicle_BIND', u'|clavicle_BIND|joint2', u'|clavicle_BIND|joint2|joint3']
What results in an error at the second rename line. Inverting the order sovles this problem, first rename the leaf node, then the ones above.
I am working on a Python program that needs to add caption texts in MS Word to Figures and Tables (with numbering). After adding the field however, the field does not appear in my Word-document until I update the field (it's just an empty space in my document, until I update the field, then it jumps to e.g. '2').
This is my code for adding the field:
def add_caption_number(self, field_code):
""" Add a caption number for the field
:argument
field_code: [string] the type of field e.g. 'Figure', 'Table'...
"""
# Set the pointer to the last paragraph (e.g. the 'Figure ' caption text)
run = self.last_paragraph.add_run()
r = run._r
# Add a Figure Number field xml element
fldChar = OxmlElement("w:fldChar")
fldChar.set(qn("w:fldCharType"), "begin")
r.append(fldChar)
instrText = OxmlElement("w:instrText")
instrText.text = " SEQ %s \* ARABIC" % field_code
r.append(instrText)
fldChar = OxmlElement("w:fldChar")
fldChar.set(qn("w:fldCharType"), "end")
r.append(fldChar)
self.last_paragraph is the last paragraph that has been added and field_code is to select whether to add a Figure or a Table caption number.
I have found an example for updating the fields, but this opens the following window upon opening the document:
def update_fields(save_path):
""" Automatically updates the fields when opening the word document """
namespace = "{http://schemas.openxmlformats.org/wordprocessingml/2006/main}"
doc = DocxTemplate(save_path)
element_updatefields = lxml.etree.SubElement(
doc.settings.element, f"{namespace}updateFields"
)
element_updatefields.set(f"{namespace}val", "true")
doc.save(save_path)
Is there a way to do this without the popup window and without adding macros to the Word document? This needs to work on MacOS and Windows btw.
The behavior described in the question is by design. Updating of fields is a potential security risk - there are some field types that can access external content. Therefore, dynamic content generated outside the Word UI needs user confirmation to update.
I know of only three ways to prevent displaying the prompt
Calculate the values and insert the field result during document generation. The fields will still be updatable, in the normal manner, but won't require updating when the document is opened the first time. (Leave out the code in the second part of the question.)
Use Word Automation Services (requires on-premise SharePoint) to open the document, which will update the fields (as in the second part of the question).
Include a VBA project that performs the field update in an AutoOpen macro. This, of course, means the document type must be macro-enabled (docm) and that macros are allowed to execute on the target installation (also a security risk, of course).
I have to verify the text of drop down list elements. How can I verify the same using python script in squish tool ?
Naive approach:
Record (then replay) selecting each of the entries. Use exception handling to log accessing individual entries and be able to proceed to test script execution.
More flexible approach:
Recording selecting one of the entries. This gives you script code to make the open the drop down and the object name of the drop down list. Then use object.children() to get all child elements of the drop down list object.
Pseudo example:
drop_down_list = waitForObject(...)
children = object.children(drop_down_list)
test.verify("Entry 1", children[0].text)
(You have to check the properties of the children to see which actual property contains the text or whatever else you want to verify.)
My list has 'n' number of files in it.
When I select a particular file in the list, the details of that particular file should be displayed in another textbox.
Above picture is the tool which I have developed .
Left hand side panel ('Input') shows the list of items present.
When I click any one of the file in the list, the details of it should be populated in the right-hand side panel ('Overall Output')
How to do that ???
You need to connect a handler to an appropriate signal of the list-widget. Since your list-widget items already contain the filename, the currentTextChanged seems a good choice:
self._ui.listWidget.currentTextChanged.connect(self.handleFilenameChanged)
...
def handleFilenameChanged(self, filename):
# get details for filename...
# load details into output widget...
# etc...
I'm experimenting with wxPython,
I have a tabbed interface (notebook) and each tab is basically a file list view (yes, I'm trying to make a file manager)
The file list inherits from wx.ListCtrl, and the tabbed interface inherits from wx.Notebook
I'm just starting .. and I had it so double clicking on a folder will cd into that folder, but I want to also change the title of the tab.
How do I do that?
I have the object that represents the file list and the title I want to set it to,
[ EDIT Notebook.SetPageText() takes a number, so I can't pass the tab object directly to it ]
my current approach is to cycle through the tabs until one of them matches my tab:
for tab_id in range(self.GetPageCount()):
if self.GetPage(tab_id) == tab:
self.SetPageText(tab_id, title)
break
This seems rather naive though, isn't there a smarter approach?
I don't know wxPython, but I assume it wraps all the methods of the C++ classes.
There is wxNotebook::GetSelection() which returns wxNOT_FOUND or the index of the selected page, which can then be used to call wxNotebook::SetPageText().
Or use wxNotebook::GetPage() with this index to check whether it is equal to tab.
I think doing something like this helps :
notebook.get_tab_label(notebook.get_nth_page(your_page_number)).set_text("Your text")
If you want to have a reference to the current tab always, you must connect the "switch-page" signal, and save the page in a variable.
As .GetPage returns a wx.Window, I think tab.Label = title should work.