Changing the title of a Tab in wx.Notebook - python

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.

Related

Is there an pysimplegui function to get the the loction of the input cursor?

I am making an application which includes a form, at some point, I have a multiline element that must be filled by the user and I need the cursor location because the user must indicate after a specific text location of the multiline element.
I have try to access to the Tkinter method : print(t.index(tk.INSERT)) , but I got stuck.
Thanks for the help.
Assuming t is your PySimpleGUI element, t.Widget should give you access to the underlying Tkinter widget.
print(t.Widget.index(tk.INSERT))

wxWidgets Python save checkbox and other elements values in a text

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.

Creating menus using wxpython

I created a menu using:
fileMenu = wx.Menu()
fileMenu.Append(ID_NEW, "&New\tCtrl+N", "Creates a new file")
I can access New by clicking it on the menu or by clicking Ctrl+N.
My question is: What is the & stand for?
if I delete it everything is still working. However all guides still use it and none of them explain what is the purpose of it.
I've read also here:
http://wxpython.org/Phoenix/docs/html/MenuItem.html#MenuItem.SetItemLabel
"An accelerator key can be specified using the ampersand & character ... Optionally you can specify also an accelerator string appending a tab character \t followed by a valid key combination"
So according to this there is no need to use & if I use \t.
can someone confirm if my conclusion is correct? I just can't figure why I always see both... there has to be a reason why everyone are using it this way.
The & characters in menu titles indicate an accelerator that you can press while the menu is open to quickly access the item. This only works when the menu is open and focused. You'll see the underscored character in the item title as a mnemonic. Same applies to the top level menu entry as well, such as 'File' or 'Edit': by standard WIMP convention you can open said menu using Alt-F (if F is the accelerator for File) and then just as quickly press 'C' if there is an accelerator for C in that menu. Quick and very little known these days.
The \t means a tab in traditional context and in menu items it will make a global shortcut in your frame that you can press without the menu being active.
The shortcut letter following & will be accessible only from the menu.
The \tCtrl+N is available from the entire app, and the short-cut is displayed next to the menu item.

Editor (Sublime, Notepad++, etc.) Paste 'around' existing text

Motivation: I was going around assigning parameters read in from a config file to variables in a function like so:
john = my_quest
terry = my_fav_color
eric = airspeed
ni = swallow_type
...
when I realized this was going to be a lot of parameters to pass. I thus decided I'd put these parameters in a nice dictionary, grail, e.g. grail['my_quest'] so that the only thing I needed to pass to the function was grail.
Question: Is there a simple way in Sublime (or Notepad++, Spyder, Pycharm, etc.) to paste grails['variable'] around those variables in one step, instead of needing to paste the front and back seperately? (I know about multiple cursors in Sublime, that does help, but I'd love to find a "highlight-variable-and-hit-ctrl-meta-shift-\" and it's done.)
Based on examples you provided, this is a simple task solvable using standard regex find/replace.
So in Notepad++, record this macro (for recording control examine Macro menu):
Press Ctrl+H to open Find/Replace dialog
Find what: = (.*)$
Replace with: = grail['\1']
Choose Regular Expression and press Replace All
If you finish recording the macro and you choose to save it, shortcut key is requested. Assign your favorite ctrl-meta-shift-\ and you are done.

listctrl new line for an data item

I have created a listctrl with some of the data in the listctrl are very long, and instead of showing all of the text it ends with .... For example Att PSSM_r1_0_T is [-10.179077,0.944198]|Att PSSM_r1_0_Y is.... How would i be able to make it so it shows all of the text. Something like
Att PSSM_r1_0_T is [-10.179077,0.944198]|Att PSSM_r1_0_Y is
[-4.820935,9.914433]|Att PSSM_r1_2_I is [-8.527803,1.953804]|Att PSSM_r1_2_K is [-12.083334,-0.183813]|Att PSSM_r1_2_V is
[-14.112536,5.857771]|1
As the text is very long I would prefer if it covered more than one line.
I don't think that is possible to do with a standard listctrl.
Try poking around at the UltimateListCtrl, being a full owner drawn listctrl it has the ability to change the way its looks far more than a standard listctrl.

Categories

Resources