Python Forum

Full Version: PyGTK3, I can't Change Button and Table Backgorund Color using modify_bg method.
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
hello, i can't change button and table bakckground color using modify_bg method i don't know how can i change. 


my code...

import gi
gi.require_version("Gtk", "3.0")
from gi.repositroy import Gtk as gtk, Gdk as gdk

btn1 = gtk.Button("example")
btn1.modify_bg(gtk.StateType.NORMAL, gdk.color_parse("red") ) #not working 
Very first link: http://bfy.tw/ArYh
http://stackoverflow.com/a/1241609

import gtk

win = gtk.Window()
win.connect("destroy", gtk.main_quit)

btn = gtk.Button("test")

#make a gdk.color for red
map = btn.get_colormap() 
color = map.alloc_color("red")

#copy the current style and replace the background
style = btn.get_style().copy()
style.bg[gtk.STATE_NORMAL] = color

#set the button's style to the one you created
btn.set_style(style)

win.add(btn)
win.show_all()

gtk.main()
@nilamo

But i use pygtk3, i hope, that works on pygtk3. Thanks. I m gonna try

Edit:
I tried. code didn't work and gave error: " 'Button' object has no attribute 'get_colormap' "

topic is still up to date

topic is still up to date
#!/usr/bin/python3
import os
import time
import gi
from gi.repository import Gtk, Gdk

class GridWindow(Gtk.Window):
    def __init__(self):
        Gtk.Window.__init__(self, title="NEOTECH")
        self.grid = Gtk.Grid()
        self.add(self.grid)
        self.btnStartTest=Gtk.Button("Iniciar Prueba")
        self.btnStartTest.connect("clicked",self.StartTest)
        self.label1 = Gtk.Label("NUMERO DE SERIE")
        self.grid.add(self.btnStartTest)
        self.grid.attach(self.label1,0,1,1,1)


    def StartTest(self,widget):
        color = Gdk.color_parse('green')
        rgba = Gdk.RGBA.from_color(color)
        self.label1.override_background_color(0,rgba)
   

win = GridWindow()
win.set_position(Gtk.WindowPosition.CENTER)
win.set_default_size(150,60)
win.set_type_hint(Gdk.WindowTypeHint.MENU)
win.connect("delete-event", Gtk.main_quit)
win.show_all()
Gtk.main()