How to change scrollbar color in pygtk? - python

I Tried the below code, the color is not reflected, Am I missing something?
#add description box beside test cases
testCaseDescWindow = gtk.ScrolledWindow()
testCaseDescWindow.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
testCaseDescWindow.get_vscrollbar().modify_fg(gtk.STATE_NORMAL,gtk.gdk.color_parse('#40515F'))
testCaseDescWindow.get_hscrollbar().modify_fg(gtk.STATE_NORMAL,gtk.gdk.color_parse('#40515F'))

You are setting the foreground color which GtkScrollbar probably does not use.
You could try to use GtkCssProvider and check which property is the correct to modify (see http://worldofgnome.org/making-gtk3-themes-part-2-the-gtk-css-and-gtk-widgets-css-files/ for list of properties)

Related

Tkinter/Ttkbootstrap Font Size After Theme Change

I'm probably just missing something obvious here but I've tried everything I know of. In my application, the user can change the theme by selecting it from a Combobox, and the theme changes. The unwanted behavior is that the font size also changes back to the default. I've tried resetting it after the theme change but the only way I can find to do it would be to destroy the entire instance and reload everything which is not preferable. Any suggestions?
I also noticed that the font size does go back to what it was set to when the theme is switched to the originally set theme. I.e. if I had it set to font size 12 and the theme as darkly when the application was first started, then the font size will go to the default when the theme is changed to anything else but will go to 12 when darkly is selected again.
Here's a MWE:
import tkinter as tk
import ttkbootstrap as tb
def change_theme(event):
t = combo.get()
style.theme_use(t)
app = tb.Window(title='Font Weirdness',
themename='darkly')
style = app.style
style.configure('.', font=('TkDefaultFont', 20))
button = tb.Button(app, text='Hello')
button.pack(pady=20)
combo = tb.Combobox(app, values=tb.Style().theme_names())
combo.pack(pady=20)
combo.bind('<<ComboboxSelected>>', change_theme)
app.position_center()
app.mainloop()
Thanks for any help!
The problem is that you're giving the current style a custom font that only applies to that style. When you do font=('TkDefaultFont', 20) you are creating a new font based on the family "TkDefaultFont". That's not a valid font family so the custom font will end up being based on the default font. The actual default font remains unchanged.
If you want to define the font once and have it be the same for all styles, you need to modify the actual default font. You can use nametofont to get the actual font object. You can then change the size of that font object with configure:
from tkinter.font import nametofont
...
default_font = nametofont("TkDefaultFont")
default_font.configure(size=30)
...
With that, you can remove your style.configure statement and all styles that use TkDefaultFont properly will use your new definition.

Python Tkinter : How to change underground / background color of Treeview, Entry and other widgets

By using wrong keyword I've stupidly struggled to change what we could call the first level of color of ttk widgets. It's not underground or underlayer but fieldbackground as most of you already know.
See below...
A little reminder :
background color is the color under the text in the widget
foreground color is the color of the text
fieldground color is the color of the place where the text will appear
This can be manage with Style:
self.MainTk = tkinter.Tk()
self.style = ttk.Style( self.MainTk )
self.style.configure("Treeview", fieldbackground = 'grey65')
self.style.configure("TEntry", fieldbackground = 'grey65')
Note that some widgets need a T before the name in the configure phase.
see https://www.pythontutorial.net/tkinter/ttk-style/ for more infos.
In Treeview, it manage the Tree mode appearence when you have a single parent collapsed.
From digging old posts it seems that this may not work with some themes under some configuration. You'll find out.
WARNING : Since the error is often made even if it's logic, the widgets must be ttk not tkinter so you must use
self.MyEntry = ttk.Entry(MainTk)
instead of
self.MyEntry = tkinter.Entry(MainTk)
or
self.MyEntry = tk.Entry(MainTk)
if you have import tkinter as tk. Here lie the most common mistake I think.

Dot in a selected radiobutton with bg, fg specified disappears

Beginner question: I'm trying to change the colour of my GUI, particularly the radiobuttons. I need inverted colours, so black background, white text.
self.radiobuttonVariable = Tkinter.IntVar()
radiobutton1 = Tkinter.Radiobutton(self, text=u'E', variable = self.radiobuttonVariable,
bg='black', fg='white' activebackground='black', activeforeground='white',
value = 1, command = self.RadioSelect)
radiobutton1.grid(column=2, row=1, sticky='ES')
This looks just fine, but when I press the button, it's the dot is there only as long I'm pressing it, disappears as soon as I let it go. The variable doesn't change, it stays on the right value, just that the dot disappears. No issue whatsoever when I remove the colour-management options. Any ideas?
As far as I can tell, specifying the foreground (presumably for the text) also sets the selected "dot color". You can set the selectcolor attribute of the element to color the "background" of the radiobutton so that you can see the white dot.
For example, selectcolor='red' in Windows:
Be aware that the manpage indicates that coloring all radio buttons with selectcolor (instead of only the selected button) may be Windows-only:
Under Windows, this color is used as the background for the indicator regardless of the select state.
That being said, I got the same effect in linux under Python 2.7 and 3.3:
I just used red to distinguish the part of the widget that selectcolor affected, you'd probably want selectcolor='black' or something a bit lighter to show the depression selectcolor='#222222' (below):

How to changes fonts using ttk themed widgets in windows

On OS X, ttk.Style().configure('TLabelframe.label', font='helvetica 14 bold') works to change the font used by the ttk.LabelFrame widget. On Windows, ttk.Style().configure('TLabelframe.label', font='arial 14 bold') has no effect other than returning the same font info to ttk.Style().lookup('TLabelframe.label','font').
I've tried different font names and formats, creating a derived style, using TkDefaultFont and just changing the size, and different widgets (TButton.label, TCheckbutton.label). So far, no matter what I've tried, it always appears to use TkDefaultFont in the default size.
Changing the font setting in python27/tcl/tk8.5/ttk/xpTheme.tcl (the default theme on windows) does change the font being displayed. Removing the -font TkDefaultFont setting from the theme settings does not change what is displayed.
Any suggestions as to how this actually works?
Edit: I hadn't tried changing the font for the Label widget before, and that one actually works.
I believe the code in this area is buggy and will open a ticket. Using 'TLableframe.Label' (note uppercase 'L' in 'Label' works. 'TButton.label' and 'TButton.Label' don't work, but just 'TButton' does; 'TCheckbutton' is the same. I was unable to change the fonts for 'TEntry' with any combination, including adding 'textarea.'
It looks like for ttk.LabelFrame, you have to create a separate ttk.Label widget, and then assign it to the LabelFrame using the labelwidget= operand. You can set whatever font/style on the Label widget that you desire and that will be reflected in the LabelFrame. Note, you don't call the geometry manager for the Label widget. Just instantiate it, then assign it to the LabelFrame.
This also means that you can assign almost any widget you want, such as a ttk.Checkbutton, if you wanted to control the state of child controls within the LabelFrame. You'd have to write the code for this, but visually, it'd enable/disable the child controls based on the state of the Checkbutton.
Source: http://infohost.nmt.edu/tcc/help/pubs/tkinter/web/ttk-LabelFrame.html

Can I make a custom pygtk tooltip stay on when the cursor is over it?

Please look at the following snippet :
import gtk
def callback(widget, x, y, keyboard_mode, tooltip):
hbox = gtk.HBox(False, 8)
button = gtk.Button('Exit Tooltip')
label = gtk.Label('Tooltip text')
hbox.pack_start(label)
hbox.pack_start(button)
hbox.show_all()
tooltip.set_custom(hbox)
return True
label = gtk.Label('Test label')
label.set_has_tooltip(True)
label.connect('query-tooltip', callback)
Here I've created a custom tooltip with a close button in it. Now I want it to stay until i click that close button. Searching google was not that helpful. besides I would also like to know the signals/events that are emitted when a tooltip is being closed.
Similar problems are handled smoothly for JQuery/JavaScript/Ajax tooltips etc. but
for gtk/pygtk there is no luck :(
Thanks in advance ...
I had this issue as well, and as far as I know, there isn't any way to determine how long a tooltip stays up.
What I did (and recommend to you) is that you make your own "tooltip" and set it's background color to yellow, or whatever color you want, via an eventbox. Make sure you don't show it yet. This is just a simplified code, as you will need to position and size this in your project yourself.
color = gtk.gdk.rgb_get_colormap().alloc_color('black')
ebTooltip = gtk.EventBox()
btnTooltip = gtk.Button("Close")
ebTooltip.add(btnTooltip)
ebTooltip.modify_bg(gtk.STATE_NORMAL, color)
Now, you just need to hide and show this eventbox via your callbacks. To show it, call...
ebTooltip.show()
And, to hide it (probably on the "clicked" event of your close button)...
ebTooltip.hide()
Hope that solves your issue!

Categories