我有一个使用Cairo 图像表面的小型 PyGI 项目,然后我使用表面图案进行缩放并在 Gtk.DrawingArea 上渲染。
我想将缩放版本写入PNG 文件。我试图用Surface.write_to_png()从原始表面写入,但它只写入原始(即非缩放)大小,所以我被困在那里。
然后我想我也许可以从 Gtk.DrawingArea 获取渲染图像并将其写入磁盘,但我还没有找到如何在 PyGI 中做到这一点(这似乎只能在 GTK+ 2 中实现 -将 gtk.DrawingArea 保存到文件)。所以我想弄清楚如何将缩放后的图像写入磁盘。
这是创建表面、放大并渲染它的代码:
def on_drawingarea1_draw (self, widget, ctx, data=None):
# 'widget' is a Gtk.DrawingArea
# 'ctx' is the Cairo context
text = self.ui.entry1.get_text()
if text == '':
return
# Get the data and encode it into the image
version, size, im = qrencode.encode(text)
im = im.convert('RGBA') # Cairo expects RGB
# Create a …Run Code Online (Sandbox Code Playgroud) 我找到了这个代码
gdk_rgba_parse ()
Run Code Online (Sandbox Code Playgroud)
这应该允许我做类似的事情
Gdk.RGBA.parse(#7F7F7F)
Run Code Online (Sandbox Code Playgroud)
错误:
TypeError: unbound method parse() must be called with RGBA instance as first argument (got str instance instead)
Run Code Online (Sandbox Code Playgroud)
使用 RGBA 颜色(百分比,从 0 到 1)
文档:
https://developer.gnome.org/gdk3/stable/gdk3-RGBA-Colors.html#gdk-rgba-parse http://www.crategus.com/books/cl-cffi-gtk/pages/gdk_fun_gdk-rgba -parse.html
但是我有点迷茫,我很难从 C 转换为 PyGOBject 并理解函数的参数.. 任何帮助将不胜感激!
由于我没有找到正确的解决方案,我制作了这个转换器:
def hex_to_rgba(value):
value = value.lstrip('#')
if len(value) == 3:
value = ''.join([v*2 for v in list(value)])
(r1,g1,b1,a1)=tuple(int(value[i:i+2], 16) for i in range(0, 6, 2))+(1,)
(r1,g1,b1,a1)=(r1/255.00000,g1/255.00000,b1/255.00000,a1)
return (r1,g1,b1,a1)
Run Code Online (Sandbox Code Playgroud)
有用..
我试图限制Gtk.CellRendererText中显示的小数位数。目前,浮点数字段显示为6个小数位,但我希望只有1个。

此测试代码应在Linux上运行:
#!/usr/bin/python3
from gi.repository import Gtk
class MyWindow(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self, title="Hello World")
self.set_default_size(200, 200)
self.liststore = Gtk.ListStore(float)
treeview = Gtk.TreeView(model=self.liststore)
self.liststore.append([9.9])
self.liststore.append([1])
xrenderer = Gtk.CellRendererText()
xrenderer.set_property("editable", True)
xcolumn = Gtk.TreeViewColumn("Float Numbers", xrenderer, text=0)
xcolumn.set_min_width(100)
xcolumn.set_alignment(0.5)
treeview.append_column(xcolumn)
self.add(treeview)
win = MyWindow()
win.connect("delete-event", Gtk.main_quit)
win.show_all()
Gtk.main()
Run Code Online (Sandbox Code Playgroud) 我正在编写一个python程序,该程序从网页获取信息并在Gnome Shell中的Notification上显示。我使用的是Arch,因此我想在启动时启动该程序,如果网页上有任何更改,它将通知我。这是我的代码:
import time
import webbrowser
import requests
from bs4 import BeautifulSoup
from gi.repository import Notify, GLib
IPS = {'Mobifone': True, 'Viettel': False, 'Vinaphone': False}
LINK = "https://id.vtc.vn/tin-tuc/chuyen-muc-49/tin-khuyen-mai.html"
def set_ips_state(ips_name, state):
global IPS
for key in IPS.iterkeys():
if key == ips_name:
IPS[key] = state
def call_webbrowser(notification, action_name, link):
webbrowser.get('firefox').open_new_tab(link)
def create_notify(summary, body, link):
Notify.init("Offer")
noti = Notify.Notification.new(summary, body, 'dialog-information')
noti.add_action('action_click', 'Read more...', call_webbrowser, link)
noti.show()
# GLib.MainLoop().run()
def save_to_file(path_to_file, string):
file = open(path_to_file, 'w')
file.write(string)
file.close()
def main():
global IPS …Run Code Online (Sandbox Code Playgroud) 现在我可以创建一个终端,但输出不用作命令.它只是将字符串打印到虚拟终端.
from gi.repository import Gtk, GObject, Vte
class TheWindow(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self, title="inherited cell renderer")
self.set_default_size(400, 200)
box = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL)
v = Vte.Terminal()
#v.connect ("child-exited", lambda term: gtk.main_quit())
length = len("echo \"string\"\n")
v.feed("echo \"string\"\n", length)
box.pack_start(v, True, True, 0)
self.add(box)
Run Code Online (Sandbox Code Playgroud)
我尝试使用这里的文档 http://developer.gnome.org/vte/0.30/,但是我在解决这些问题时遇到了一些麻烦.我根本找不到关于vte for python gtk3的任何文档.
主要是我只想弄清楚如何在虚拟终端中获取命令提示符,以便它接受来自python gtk3接口内部的命令.
我正在尝试从最初用于另一个(可能是基于 Debian 的)Linux 发行版的 github 存储库安装基于 Python 的软件包。
程序好像用的是Python2,导入如下:
import argparse
import logging
import Queue
import collections
import ConfigParser
import os
import socket
import random
import gi
gi.require_version('Notify', '0.7')
from gi.repository import Notify
from multiprocessing import Process, Queue as MPQueue, Event
...
def notif(msg):
Notify.init("TheProg")
notice = Notify.Notification.new("Critical !", msg)
notice.set_urgency(2)
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试运行该程序时,我得到了这个:
$ python2 main.py -h
Traceback (most recent call last):
File "main.py", line 11, in <module>
gi.require_version('Notify', '0.7')
File "/usr/lib/python2.7/dist-packages/gi/__init__.py", line 100, in require_version
raise ValueError('Namespace %s not available' …Run Code Online (Sandbox Code Playgroud)