In the following sample code, I have a 2 columns Gtk.Grid that contains a Gtk.Switch. How can I prevent this switch from filling the entire cell?
#!/bin/python3
import gi
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk
class MyWindow(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self, title="Foo")
self.set_border_width(12)
my_grid = Gtk.Grid()
my_grid.set_column_spacing(12)
my_grid.set_row_spacing(6)
self.add(my_grid)
first_label = Gtk.Label(label="a first label")
first_label.set_xalign(1)
my_grid.add(first_label)
my_button = Gtk.Button(label="a button quite large")
my_grid.add(my_button)
second_label = Gtk.Label(label="a second label")
second_label.set_xalign(1)
my_grid.attach(second_label, 0, 1, 1, 1)
my_switch = Gtk.Switch()
my_grid.attach(my_switch, 1, 1, 1, 1)
win = MyWindow()
win.connect("destroy", Gtk.main_quit)
win.show_all()
Gtk.main()
Thanks!
What is happening there is that using attach():
my_grid.attach(my_switch, 1, 1, 1, 1)
will set the alignment flag to Gtk.Align.FILL
adding the following
my_switch.props.halign = Gtk.Align.START
Will make sure that the switch does not fill the whole space, you can use Gtk.Align.CENTER or Gtk.Align.END if you prefer.
After reading again the Gtk-3.0 documentation I found that widgets have a property called halign which in this case seems to have value FILL by default.
The solution is simply to add the following line to indicate that halign should be START for the switch:
my_switch.set_halign(Gtk.Align(1))
Related
I am trying to put an entry widget with a label in one row using a horizontal box. However, the label expands vertically. It takes the initial height of the window and does not allow to shrink below it. I have tried all possible combinations of fill, expand, alignment etc. but it does not change.
How to fix it?
#!/usr/bin/env python3
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, Gdk
class MyWindow(Gtk.Window):
def __init__(self):
super(MyWindow, self).__init__()
self.init_ui()
def init_ui(self):
hbox = Gtk.Box(Gtk.Orientation.HORIZONTAL, 6)
hbox.set_homogeneous(True)
hbox.set_baseline_position(Gtk.BaselinePosition.CENTER)
entry = Gtk.Entry()
entry.modify_bg(Gtk.StateType.NORMAL, Gdk.Color(6400, 6400, 6440))
entry.connect("key-release-event", self.on_key_release)
hbox.pack_start(entry, False, False, 10)
self.label = Gtk.Label("Hello")
self.label.set_width_chars(15)
self.label.modify_bg(Gtk.StateType.NORMAL, Gdk.Color(40000, 40000, 40000))
hbox.pack_start(self.label, False, False, 10)
self.add(hbox)
self.set_title("Entry")
self.set_size_request(350, 250)
self.connect("destroy", Gtk.main_quit)
self.show_all()
def on_key_release(self, widget, event):
self.label.set_text(widget.get_text())
win = MyWindow()
Gtk.main()
Add another vertical box before the horizontal box:
#!/usr/bin/env python3
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, Gdk
class MyWindow(Gtk.Window):
def __init__(self):
super(MyWindow, self).__init__()
self.init_ui()
def init_ui(self):
hbox = Gtk.Box(orientation = Gtk.Orientation.HORIZONTAL, spacing = 6)
hbox.set_homogeneous(True)
hbox.set_baseline_position(Gtk.BaselinePosition.CENTER)
entry = Gtk.Entry()
entry.modify_bg(Gtk.StateType.NORMAL, Gdk.Color(6400, 6400, 6440))
entry.connect("key-release-event", self.on_key_release)
hbox.pack_start(entry, False, False, 10)
self.label = Gtk.Label("Hello")
self.label.set_width_chars(15)
self.label.modify_bg(Gtk.StateType.NORMAL, Gdk.Color(40000, 40000, 40000))
hbox.pack_start(self.label, False, False, 10)
vbox = Gtk.Box(orientation = Gtk.Orientation.VERTICAL)
vbox.pack_start(hbox, False, False, 0)
self.add(vbox)
self.set_title("Entry")
self.set_size_request(350, 250)
self.connect("destroy", Gtk.main_quit)
self.show_all()
def on_key_release(self, widget, event):
self.label.set_text(widget.get_text())
win = MyWindow()
Gtk.main()
As a side note, using non-keyword arguments does not always work properly in Python3 and Gtk, instead of hbox = Gtk.Box(Gtk.Orientation.HORIZONTAL, 6), use hbox = Gtk.Box(orientation = Gtk.Orientation.HORIZONTAL, spacing = 6)
I have a Gtk.Grid to which I want to add a row with a combo box, which is initialized with a list. However, my combo box is empty. Can anyone see what I'm missing here?
I used this tutorial as reference
queryTypes = ["Name", "Grade", "Year", "Faculty"]
queryStore = Gtk.ListStore(str)
for qt in queryTypes:
queryStore.append([qt])
window = builder.get_object("mainWindow")
grid = builder.get_object("queryGrid")
grid.nRows = 1
combox = Gtk.ComboBox.new_with_model(queryStore)
grid.add(combox)
window.show_all()
Gtk.main()
Your combobox is missing a renderer.
queryTypes = ["Name", "Grade", "Year", "Faculty"]
queryStore = Gtk.ListStore(str)
for qt in queryTypes:
queryStore.append([qt])
window = builder.get_object("mainWindow")
grid = builder.get_object("queryGrid")
grid.nRows = 1
combox = Gtk.ComboBox.new_with_model(queryStore)
renderer_text = Gtk.CellRendererText()
combox.pack_start(renderer_text, True)
combox.add_attribute(renderer_text, "text", 0)
grid.add(combox)
window.show_all()
Gtk.main()
How can I get the flex table (flex3) to align at the top in this Ui?
function doGet(e) {
var app = UiApp.createApplication();
var flex1 = app.createFlexTable().setBorderWidth(1);
var flex2 = app.createFlexTable().setBorderWidth(1).setWidget(0, 0, app.createLabel('flex2'))
.setWidget(1, 0, app.createTextArea().setHeight(400).setText('Text area in flex2'));
var flex3 = app.createFlexTable().setBorderWidth(1).setWidget(0, 0, app.createLabel('flex3'));
flex1.setWidget(0, 0, flex2).setWidget(0, 1, flex3);
app.add(flex1);
return app;
}
Was expecting...
var flex1 = app.createFlexTable().setBorderWidth(1).setStyleAttribute('vertical-align', 'top');
or
var flex1 = app.createFlexTable().setBorderWidth(1).setColumnStyleAttribute(1, 'vertical-align', 'top');
to work.
Also, using a flow panel instead of a flex for the root didn't work as expected either. That stacked the 2 flex vertically and not left to right.
Adding this line towards the bottom did the trick for me -
flex1.setStyleAttribute(0, 1, 'vertical-align', 'top');
Not sure why the global set style or set column style didn't work. However, setting it on a specific row/column seem to work. Let me know.
This might have to end up on the Issue Tracker, please check there and log an issue.
I am trying to set a colour to highlight a button. However, the modify_fg method only seems to set the focus ring's colour. modify_bg works as expected.
This is my code:
use Gtk2 qw/-init/;
my $window = Gtk2::Window->new;
$window->set_title("Window!");
my $button = Gtk2::Button->new("Coloured _button");
# does not affect text
$button->modify_fg(normal => Gtk2::Gdk::Color->new(0xffff, 0, 0));
$window->add($button);
$window->show_all;
Gtk2->main;
I'm also interested whether there is a standard colour for this sort of highlight, to blend in with the user's theme.
you can get the button's child label and modify its foreground, below is an example (python, let me know if there are troubles converting it to perl)
import gtk
class TestWindow:
def __init__(self):
window = gtk.Window(gtk.WINDOW_TOPLEVEL)
box = gtk.VBox()
button0 = gtk.Button("Test Button")
label0 = button0.get_children()[0]
label0.modify_fg(gtk.STATE_NORMAL, gtk.gdk.color_parse('red'))
button1 = gtk.Button(stock=gtk.STOCK_ABOUT)
alignment = button1.get_children()[0]
hbox = alignment.get_children()[0]
image, label1 = hbox.get_children()
label1.modify_fg(gtk.STATE_NORMAL, gtk.gdk.color_parse('blue'))
box.add(button0)
box.add(button1)
window.add(box)
window.set_size_request(200, 200)
window.show_all()
def close_application(self, widget, event, data=None):
gtk.main_quit()
return False
if __name__ == "__main__":
TestWindow()
gtk.main()
hope this helps, regards
All previous helped a lot...
All the following works ok!
my $beige = Gtk2::Gdk::Color->new (0xFFFF,0xFFFF,0xCCCC);
my $azul = Gtk2::Gdk::Color->new (0,0,0xCCCC);
my $rojo = Gtk2::Gdk::Color->new (0xCCCC,0,0);
$mw->modify_bg ("normal", $beige);
my $butOk = Gtk2::Button->new_with_label(" 🗹 ¡Ya Terminé!");
($butOk->get_children)[0]->modify_fg("normal", $azul);
($butOk->get_children)[0]->modify_font(Pango::FontDescription->from_string ("Serif Bold 27"));
my $imgBTN = Gtk2::Button->new();
my $img = Gtk2::Image->new_from_file("$imagen");
$imgBTN->set_property("image"=>$img);
I found this way:
GdkPixmap *backPixMap = gdk_pixmap_create_from_xpm ( window , NULL , NULL , fileName );
gdk_window_set_back_pixmap( GTK_WIDGET( window )->window , backPixMap , FALSE );
but it seems that GdkPixmap is obsolete now...
So, with GTK3, how can I set the background image of a GtkWindow?
You just need to use an Overlay. Following is an example.
class Window(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self)
self.overlay = Gtk.Overlay()
self.add(self.overlay)
self.bg = Gtk.Image()
# Load file. Optionally scale it for window
self.bg.set_from_file(BACKGROUND_IMAGE)
# Wrapping in the Scrollable make it resizable.
scrollable_wrapper = Gtk.ScrolledWindow()
scrollable_wrapper.add(self.bg)
scrollable_wrapper.set_size_request(700, 500)
self.overlay.add(scrollable_wrapper)
text = Gtk.Label("Test")
self.overlay.add_overlay(text)
self.connect('destroy', lambda w: Gtk.main_quit())
self.show_all()
Window()
Gtk.main()