开罗示例适用于Python 2.7,但不适用于Python 3 [英] Cairo example works with Python 2.7 but does not work in Python 3
问题描述
最简单的例子。我们通过Gtk创建一个窗口,在其中添加用于绘制Gtk.DrawingArea的区域,然后在其中通过开罗绘制文本。
The simplest example. We create a window by means of Gtk, we add there area for drawing of Gtk.DrawingArea and on it we draw the text by means of Cairo.
示例:
#!/usr/bin/env python
from gi.repository import Gtk
import cairo
class MyWindow (Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self, title='MyWindow')
darea = Gtk.DrawingArea()
darea.connect('draw', self.on_draw)
self.add(darea)
def on_draw(self, widget, ctx):
ctx.set_source_rgb(0, 0, 0)
ctx.select_font_face("Sans", cairo.FONT_SLANT_NORMAL,
cairo.FONT_WEIGHT_NORMAL)
ctx.set_font_size(20)
ctx.move_to(10, 20)
ctx.show_text("Text...")
win = MyWindow()
win.connect('delete-event', Gtk.main_quit)
win.show_all()
Gtk.main()
在我身上,一切都完美地在Python 2.7,但仅需要为Python3更改Python,并且不再绘制文本。
可能有问题吗?
At me everything perfectly works at Python 2.7, but only it is necessary to change Python for Python3 and the text isn't drawn any more. In what there can be a problem?
推荐答案
遇到同样的问题……原来你会得到
Had the same problem... turns out you'll get no complaints if you're missing a few packages.
尝试apt-get install python3-gi-cairo并再次尝试。为我工作(包括您的代码)。
try apt-get install python3-gi-cairo and give it another try. Worked for me (including your code).
这篇关于开罗示例适用于Python 2.7,但不适用于Python 3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!