Convert a Gtk Python Script to C

Convert a GTK python script to C

I tested this and it does work, but there might be a simpler way to go from GdkPixbuf to a png this was just the first one I found. (There's no gdk_pixbuf_save())

#include <unistd.h>
#include <stdio.h>
#include <gdk/gdk.h>
#include <cairo.h>

int main(int argc, char **argv)
{
gdk_init(&argc, &argv);

GdkWindow *w = gdk_get_default_root_window();

gint width, height;
gdk_drawable_get_size(GDK_DRAWABLE(w), &width, &height);

GdkPixbuf *pb = gdk_pixbuf_get_from_drawable(NULL,
GDK_DRAWABLE(w),
NULL,
0,0,0,0,width,height);

if(pb != NULL) {
cairo_surface_t *surf = cairo_image_surface_create(CAIRO_FORMAT_RGB24,
width, height);
cairo_t *cr = cairo_create(surf);
gdk_cairo_set_source_pixbuf(cr, pb, 0, 0);
cairo_paint(cr);
cairo_surface_write_to_png(surf, "screenshot.png");
g_print("Screenshot saved to screenshot.png.\n");
} else {
g_print("Unable to get the screenshot.\n");
}
return 0;
}

you'd compile like this: (assuming you save it as screenshot.c)

gcc -std=gnu99 `pkg-config --libs --cflags gdk-2.0` screenshot.c -o screenshot

Edit: the stuff to save the pixbuf could also look like: (note I didn't try this out, but it's only one line...) Thanks to kaizer.se for pointing out my fail at doc reading :P

gdk_pixbuf_save(pb, "screenshot.png", "png", NULL, NULL);

How can I convert a (Py)Gtk Gio.ThemedIcon to anything usable in (Py)Qt5 (QImage, QIcon, etc)?

The ThemedIcon has the get_names() function that returns a list of possible theme names. You can use the static fromTheme() to get the icon in Qt:

app = app_list[0]
g_icon = app.get_icon()
for name in g_icon.get_names():
icon = QtGui.QIcon.fromTheme(name)
if not icon.isNull():
break

Translate GTK UI written in Glade and Python

Code works for me when I use correct encoding in place of word CHARSET in en.po - ie. UTF-8

"Content-Type: text/plain; charset=UTF-8\n". 

Sample Image


BTW:

When I use in console/terminal

msgfmt -c en.po -o myapp.mo

then it even shows warning

en.po: warning: Charset "CHARSET" is not a portable encoding name.
Message conversion to user's charset might not work.

and it confirms that CHARSET has to be changed.


It shows also warnings for other values - PACKAGE VERSION, YEAR-MO-DA HO:MI+ZONE, FULL NAME <EMAIL@ADDRESS>, LANGUAGE
- but they are not so important.

po/en.po:7: warning: header field 'Project-Id-Version' still has the initial default value
po/en.po:7: warning: header field 'PO-Revision-Date' still has the initial default value
po/en.po:7: warning: header field 'Last-Translator' still has the initial default value
po/en.po:7: warning: header field 'Language-Team' still has the initial default value
po/en.po:7: warning: header field 'Language' still has the initial default value

EDIT:

it seems code sets LC_ALL - locale.setlocale(locale.LC_ALL, ...) but in my system (Linux Mint 19.2 based on Ubuntu 18.04) I have also LANG=pl_PL.UTF-8, LANGUAGE=pl_PL:pl and maybe it makes difference. But I can't set it in code locale.setlocale(locale.LANG, ...)

EDIT:

You confirmed that variable LANGUAGE was the problem.

If LANGUAGE is hu:en or hu then .mo file has to be in folder hu instead of hu_HU


BTW:

I checked I have also folder hu in /usr/share/locale/ but when I run

locale -a | grep hu

then it shows me

hu_HU
hu_HU.UTF-8

And locale.setlocale(locale.LC_ALL, 'hu') gives me error but locale.setlocale(locale.LC_ALL, 'hu_HU') runs without error.

I created two folders with different words mo/hu_HU/... and mo/pl_PL/... and even if I use locale.setlocale(locale.LC_ALL, 'hu_HU') I see words from pl_PL . But when I remove locale.setlocale() then I see English words.

To see words from hu_HU I have to run it in console as

 LANGUAGE=hu_HU python test.py

Getting a GTK Label to dislay an int in C

I modified your code to work as you described:

#include <gtk/gtk.h>
#include <string.h>
#include <stdlib.h>

void updateLabel(GtkLabel *sum, int num)
{
gchar *display;
display = g_strdup_printf("%d", num); //convert num to str
gtk_label_set_text (GTK_LABEL(sum), display); //set label to "display"
g_free(display); //free display
}

void buttonAdd (GtkButton *button, GtkLabel *sum)
{
int num = atoi(gtk_label_get_text(sum));
num += 1;
g_print("%d\n",num);
updateLabel(GTK_LABEL(sum), num);
}

void buttonSub (GtkButton *button, GtkLabel *sum)
{
int num = atoi(gtk_label_get_text(sum));
num -= 1;
g_print("%i\n",num);
updateLabel(GTK_LABEL(sum),num);

}

int main(int argc, char **argv)
{
GtkWidget *window;
GtkWidget *addButton;
GtkWidget *subButton;
GtkWidget *grid;
GtkWidget *sum;

gtk_init (&argc,&argv);

//Declarations
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
sum = gtk_label_new ("0");
grid = gtk_grid_new ();
addButton = gtk_button_new_with_label ("Add Value");
subButton = gtk_button_new_with_label ("Sub Value");

//Set Properties
gtk_container_set_border_width (GTK_CONTAINER(window), 20);
gtk_widget_set_size_request (GTK_WIDGET(window), 150, 100);
gtk_label_set_selectable (GTK_LABEL(sum), TRUE);
gtk_grid_set_row_spacing (GTK_GRID(grid), 4);
gtk_grid_set_column_spacing (GTK_GRID(grid), 4);
gtk_container_add (GTK_CONTAINER(window), grid);

//Fill the grid with shit (x, y, h, v)
gtk_grid_attach (GTK_GRID(grid), addButton, 0, 0, 1, 1);
gtk_grid_attach (GTK_GRID(grid), subButton, 1, 0, 1, 1);
gtk_grid_attach (GTK_GRID(grid), sum, 0, 2, 2, 1);

gtk_widget_show_all (window);

g_signal_connect(G_OBJECT(window), "destroy", G_CALLBACK(gtk_main_quit), NULL);
g_signal_connect(G_OBJECT(addButton), "clicked", G_CALLBACK(buttonAdd), sum);
g_signal_connect(G_OBJECT(subButton), "clicked", G_CALLBACK(buttonSub), sum);

gtk_main();

return 0;
}

From the beginning:

  • Although a char can be used as an int, you cannot convert a whole int to string simply by char display = n; as each char has it's own value associated with a character they represent (http://www.asciitable.com/). A string (char * or char[]) is made of a table of chars.

  • Button's signal function buttonAdd() and buttonSub contains two parameters and not three: the first is a pointer of the gtk widget that caused the signal and the second parameter is a pointer passed in the last parameter from g_signal_connect() function. Normally we'd have a lot of data to pass trough the pointer so in that case we'd make a struct variable, fill it and pass that through, but in your case it's enough to just pass the gtk label, because it already contains the string which we can read and increase/decrease it.

  • I used atoi() to convert the string to int, but for a more robust solution it's better to use strtol, but for your example atoi() is more than enough.

  • You cannot use GTK_LABEL() as a parameter, because it's a macro and not a type.

How to convert .glade file to python class?

The user interfaces create with glade are saved as xml file: using gtkbuilder, this file can be used with many programming languages, like c++ or python.
Pygtk lets you create application with user interfaces, based on glade file.But you have to do all by hand.



Related Topics



Leave a reply



Submit