Python GTK+ Canvas

2024/9/8 8:49:42

I'm currently learning GTK+ via PyGobject and need something like a canvas. I already searched the docs and found two widgets that seem likely to do the job: GtkDrawingArea and GtkLayout. I need a few basic functions like fillrect or drawline ... In fact these functions are available from c but I couldn't find directions how to use them from python. Can you recommend a tutorial or manpage that deals with their python equivalents ?

If you have a better idea how to get something similar to a canvas every tip would be appreciated. I'm still learning and as long as it can be embedded within my Gtk application I'd be content with any solution.

Answer

In order to illustrate my points made in the comments, let me post a quick'n'dirty PyGtk example that uses a GtkDrawingArea to create a canvas and paints into it using cairo

CORRECTION: you said PyGObject, that is Gtk+3, so the example is as follows (the main difference is that there is no expose event, instead it is draw and a cairo context is already passed as a parameter):

#!/usr/bin/python
from gi.repository import Gtk
import cairo
import mathdef OnDraw(w, cr):cr.set_source_rgb(1, 1, 0)cr.arc(320,240,100, 0, 2*math.pi)cr.fill_preserve()cr.set_source_rgb(0, 0, 0)cr.stroke()cr.arc(280,210,20, 0, 2*math.pi)cr.arc(360,210,20, 0, 2*math.pi)cr.fill()cr.set_line_width(10)cr.set_line_cap(cairo.LINE_CAP_ROUND)cr.arc(320, 240, 60, math.pi/4, math.pi*3/4)cr.stroke()w = Gtk.Window()
w.set_default_size(640, 480)
a = Gtk.DrawingArea()
w.add(a)w.connect('destroy', Gtk.main_quit)
a.connect('draw', OnDraw)w.show_all()Gtk.main()
https://en.xdnf.cn/q/73260.html

Related Q&A

Python, can someone guess the type of a file only by its base64 encoding?

Lets say I have the following:image_data = """iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg=="""This i…

Extract only body text from arXiv articles formatted as .tex

My dataset is composed of arXiv astrophysics articles as .tex files, and I need to extract only text from the article body, not from any other part of the article (e.g. tables, figures, abstract, title…

why is python reusing a class instance inside in function

Im running a for loop inside a function which is creating instances of a class to test them. instead of making new classes it appears to be reusing the same two over and over.Is there something Im miss…

How to set locale in Altair?

Im successfully creating and rendering a chart in Altair with a currency prefix ($), but I need this to be set to GBP (£). I know that theres a Vega-lite formatLocale which can be set, but I cant …

Show/hide a plots legend

Im relatively new to python and am developing a pyqt GUI. I want to provide a checkbox option to show/hide a plots legend. Is there a way to hide a legend? Ive tried using pyplots _nolegend_ and it ap…

Difference between iterating over a file-like and calling readline

I always thought iterating over a file-like in Python would be equivalent to calling its readline method in a loop, but today I found a situation where that is not true. Specifically, I have a Popend p…

Creating `input_fn` from iterator

Most tutorials focus on the case where the entire training dataset fits into memory. However, I have an iterator which acts as an infinite stream of (features, labels)-tuples (creating them cheaply on …

A Python one liner? if x in y, do x

numbers = [1,2,3,4,5,6,7,8,9] number = 1Can I write the following on one line?if number in numbers:print numberUsing the style of ruby:puts number if numbers.include?(number)I have tried:print number…

Adjusting the ticks to fit within the figure

I have the following matplotlib code which all it does is plots 0-20 on the x-axis vs 0-100 on the y-axisimport matplotlib.pyplot as plt fig = plt.figure() ax = fig.add_subplot(111) ax.plot(range(20)) …

Python ctypes: pass argument by reference error

I have a C++ function that I want you call in Python 2.7.12, looking like this:extern "C" {double* myfunction(double* &y, double* &z, int &n_y, int &n_z, int a, int b){vector&…