在Python中定义GLib.Variant

时间:2017-02-05 13:28:38

标签: python glib dbus gvariant

我正在尝试在Python中定义GLib.Variant数据类型,以便将其与pydbus库一起使用。这是我尝试这样做的:

#!/usr/bin/python

from gi.repository import GLib
from pydbus import SessionBus

var1 = GLib.Variant.new_variant('draw-cursor', False)
var2 = GLib.Variant.new_variant('framerate', 30)

bus = SessionBus()
calling = bus.get('org.gnome.Shell.Screencast', '/org/gnome/Shell/Screencast')

calling.Screencast('out.webm', {var1, var2})

然而它说TypeError: GLib.Variant.new_variant() takes exactly 1 argument (2 given)。我可以清楚地看到。但是,我如何为我将要定义的值分配值?它不应该是像{'framerate': 30}这样的词典吗?

2 个答案:

答案 0 :(得分:1)

options参数的类型为import plotly import numpy n = 50000 #number of random points for heatmaps/histograms #location of the annotations anno_x = [0.05, 0.6] anno_y = [0.8, 0.00] #the ranges of all subplots, [row][x, y] row_range = list() row_range.append([[0, 7.25], [35, 105]]) row_range.append([[25, 225], [425, 775]]) row_range.append([[0, 5.25], [-5.25, 2.75]]) #label annotations for each subplot labels = ['(a)', '(b)', '', '', '(c)', '(d)'] #create the subplot fig = plotly.tools.make_subplots(rows=3, cols=2, subplot_titles=(('',) * 6)) #assign the annotations annotations = list() for i, label in enumerate(labels): if label: annotations.append(dict(text=label, x=anno_x[i % 2], y=anno_y[i // 3], xref='paper', yref='paper', showarrow=False)) else: annotations.append(dict(showarrow=False, text='')) fig['layout'].update(annotations=annotations) #let's create some random data trace1 = plotly.graph_objs.Scatter( x=[i for i in range(8)], y=[100 - i * numpy.random.randint(7) for i in range(8)], name='', showlegend=False, ) fig.append_trace(trace1, 1, 1) trace2 = plotly.graph_objs.Scatter( x=[i for i in range(8)], y=[100 - i * 5 + numpy.random.randint(2) for i in range(8)], name='', showlegend=False, ) fig.append_trace(trace2, 1, 2) #some random points for the 1st heatmap/histogram trace3 = plotly.graph_objs.Histogram2d( x=numpy.random.normal(loc=80, scale=50, size=n), y=numpy.random.normal(loc=525, scale=200, size=n), autobinx=False, autobiny=False, xbins=dict(start=row_range[1][0][0], end=row_range[1][0][1], size=5), ybins=dict(start=row_range[1][1][0], end=row_range[1][1][1], size=5), colorbar=dict(showticklabels=False) ) fig.append_trace(trace3, 2, 1) trace4 = plotly.graph_objs.Histogram2d( x=numpy.random.normal(loc=50, scale=100, size=n), y=numpy.random.normal(loc=550, scale=200, size=n), showscale=False, colorbar=dict(showticklabels=False) ) fig.append_trace(trace4, 2, 2) trace5 = plotly.graph_objs.Scatter( x=[i for i in range(6)], y=[0 - i + numpy.random.rand() for i in range(6)], name='', showlegend=False, ) fig.append_trace(trace5, 3, 1) trace6 = plotly.graph_objs.Scatter( x=[i for i in range(6)], y=[2 - i * 0.3 for i in range(6)], name='', showlegend=False, ) fig.append_trace(trace6, 3, 2) for i in range(1, 7): fig['layout']['xaxis' + str(i)].update(range=row_range[((i - 1) // 2)][0]) fig['layout']['yaxis' + str(i)].update(range=row_range[((i - 1) // 2)][1]) plot_url = plotly.plotly.plot(fig) ,因此您可能需要明确提供类型:

a{sv}

答案 1 :(得分:0)

第二次失败(AttributeError: 'Variant' object has no attribute 'items')似乎是因为pydbus希望你传入dict,而不是GLib.Variant,并且无条件地包裹你通过的任何内容它在GLib.Variant。这意味着它会尝试从items变体中获取options,但由于GLib.Variant不支持,因此失败了。

此代码适用于pydbus

calling.Screencast('out.webm', {
    'draw-cursor': GLib.Variant('b', False),
    'framerate': GLib.Variant('i', 30)
})