pygtk textview tab width? - python

Is there a way to set the tab width of the pygtk TextView widget to be something other than 8spaces?
Right now I tried textview.set_tabs(pango.TabArray(4, False)) However, it seems like I misunderstood the docs. What the actual way to do it?
Thanks

See the docs for pango.TabArray: your statement pango.Tabarray(4, False) creates a tab array with four tab stops, all at position 0 points. What you want is this:
tabs = pango.TabArray(1, True)
tabs.set_tab(0, pango.TAB_LEFT, desired_tab_width)
textview.set_tab(tabs)
The value of desired_tab_width is not in spaces, however. See the function set_tab_stops_internal() in this code from GtkSourceView to see how they calculate the width of a space.

Related

Clear line to the right of the text displayed by progressbar.FormatCustomText (python)

I am using progressbar2 with FormatCustomText. The text is parameterized (eg "%s") and the width of the string to display varies.
Let's say on the first iteration, the substituted text is "AA." On the second iteration, the text is "B."
progressbar displays BA on the second iteration instead of B.
Is there a way to clear everything to the right of the cursor to the end of the line after B? Normally, this is done with control-K (chr(11)).
Unfortunately, adding chr(11) to the end of the string given to FormatCustomText causes a newline character to be output for each iteration.
Any other ideas?
Print "\033[K" at the end
progressbar.ProgressBar(widgets=[format_custom_text, "\033[K"])
Ideally the text to substitute should have a maximum length based on a percentage of the width of the terminal. I found a way to get the width but there is probably a better way! (How to get Linux console window width in Python)
This works in python3+:
import shutil
shutil.get_terminal_size((80, 20)) # Provide default values
There is a better answer on github

Tkinter Text widget - identify line by y-coord

is there any method to identify row/column in tkinter.Text widget in tkinter? I mean something like methods identify_row(event.y) and identify_column(event.x).
I want to highlight the line bellow cursor and I need it to work with disabled Text widget.
I was thinking of getting the height and count the line number from the current y coord and the height of the line, but I thought, there might be a better way.
So, is there?
Thank you for any advice.
Found it, it works for me:
def __onLClicked(self, event):
linestart = self.index("#{0},{1} linestart".format(event.x, event.y))
lineend = self.index("{} lineend".format(linestart))
self.tag_remove("current_line", 1.0, "end")
self.tag_add("current_line", linestart, lineend)
Only one detail, it unfortunately doesn't higlight all lines the same, because each line has different length. If anyone knows how to highlight full 'width' (the length of the longes line I guess) for every line without adding extra spaces...that'd be great!

Widget alignment in cell pyqt

I'm developing some tool in PyQT4 and Python 2.7 and I stuck in a little problem. I've 3 buttons in stored in widget and that widget is in cell in the table (QTableWidget). So my problem is that I can't align widget to top of cell and tool doesn't resize row height to defined in widget minimum/fixed height.
Here is how it looks now with method resizeRowToContents.
And I want something like this
I did something similar with fixed row height but its not the way I want.
I solved this problem in quite simply way.
First of all I changed margin of layout to 0px (later I changed that to 3px to have padding) and this made my alignment exactly to top, left corner. Next step was set minimum size for each row and that I made by following line:
self.verticalHeader().setMinimumSectionSize(30)
added in constructor.
After that I received exactly what I want:
widget with some pading from top and bottom
not fixed size of rows so multiline text in item is allowed as well

Finding The Current Size Of A tkinter.Text Widget

I am trying to create a roguelike using the Text widget.
I have figured out a few things, namely that I can set the size of the widget using width and height options and that I can find the pixel height or width of said widget. However, what I want to do is have the widget resizable (pack(expand="yes", fill="both")) but be able to refresh the displayed text on a resize. Is there a way to get the character dimensions when the widget is running without resorting to winfo_width() and math based on pixel dimensions of characters?
I've run into that exact same problem a couple times jaccarmac, and to my knowledge there is no way to find the width of a string of characters. Really the only way is to use the winfo_ commands: width, height, geometry. However, it kind of sounds like you just want to make sure that all of the text is displayed if you change the label and add more text. If that is the case, you don't have to worry about it. That should all be taken care of by the widgets themselves. If you don't see them expanding to show all of your label, that usually means one of the widgets containing that label is not set to expand (either using expand=YES with .pack, or columnconfigure(i, weight=1) for .grid).
A final thought; in the pack arguments make sure it's YES, and not "yes". That uppercase YES is not a string, but a variable name defined by Tkinter.
There is no way to automatically get the width in characters, but it's easy to calculate, assuming you're using a fixed width font. One way to do this is to use the font_measure method of a font object. Use font_measure to get the width of a '0' (or any other character for that matter; I think tk users zero internally, not that it matters with a fixed width font), then use this in your calculations.
This is an old question, but after doing some research I've found that there actually is a way to get height/width info directly without maths or playing with font widths using the Text widget's cget() method:
text_widget = tk.Text()
width_in_char = text_widget.cget('width')
height_in_char = text_wdiget.cget('height')
Since the Text widget stores its height and width configuration in characters, simply querying for those parameters will give you what you're looking for.

How do you change alternating background row colors of a gtk.TreeView in pygtk?

I'm trying to change the alternating background color of a treeview. I know that this should normally be left up to the theme, but I'd like to override to test the gtk Style functionality. According to the treeview documentation here, I learned that the TreeView has several style options that are Read-only, including "even-row-color", "odd-row-color" and "allow-rules"(which according to the documentation, allows drawing of even and odd row colors). And I know that in order to override those Read-only settings I've got to change the style in a gtkrc-style file or string.
So my string for a treeview looks like:
gtk.rc_parse_string( """
style "custom-treestyle"{
GtkTreeView::odd-row-color = "#00CBFF"
GtkTreeView::even-row-color = "#90EE90"
GtkTreeView::allow-rules = 1
}
widget "*custom_treeview*" style "custom-treestyle"
""")
treeview.set_name("custom_treeview" )
This parses without error and the result is that the even-row-color gets applied to both even and odd rows.
EDIT: I discovered from some more testing that my parse string must be getting overridden from some other style settings.
print treeview.style_get_property( 'allow-rules' )
print treeview.style_get_property( 'odd-row-color')
print treeview.style_get_property( 'even-row-color')
Gives the result:
True
None
None
Which are all default settings. Normally I would think that it's simply not parsing the string and setting the appropriate values, but in this case the background color does change to the color I specified( only it paints every row's background to one color).
What am I doing wrong?
I finally figured out my issue with this styling method.
The 'allow-rules' style property isn't the only property that tells the treeview to color the rows in alternating colors. According to the documentation, the treeview has another property that hints to the engine that it should draw rows in alternating colors. The 'rules-hint' property, False by default, when True successfully colors my TreeView in alternating odd/even row colors!
So, in code, add this line to the code in the question:
treeview.set_rules_hint( True )
And that's it, a TreeView with alternating colored rows!

Categories

Resources