简体   繁体   中英

Python3, Gtk3, adjustment value randomly changing

Given the following python code, it should create a GtkAdjustment with a value of 123.

It is (apparently randomly) getting different values. Either the value 123.0 the min value 1.0 or 0.0 (Constant, not the parameter).

#!/usr/bin/python3
from gi.repository import Gtk

adjustment = Gtk.Adjustment(123,1,200,1,10,0)
print(str(adjustment.get_value()))

What is going on?

Edit: The following C program acts as expected, so it's specific to python:

#include <gtk/gtk.h>
#include <stdio.h>

int main(int argc, char *argv[]){
  gtk_init (&argc, &argv);

  GtkAdjustment * adjustment = gtk_adjustment_new(123.0,1.0,200.0,1.0,10.0,0.0);
  printf("%f\n", gtk_adjustment_get_value(adjustment));

  return 0;
}

The problem is that you are using the generic constructor. In C terms, it is first constructing the object with g_object_new and then setting the values one by one. Apparently that does not happen in a deterministic order.

The better approach is to use keyword arguments to fix the values to names

adjustment = Gtk.Adjustment(value=123, lower=1, upper=200, ...)

or use the actual constructor function that has a fixed order of arguments

adjustment = Gtk.Adjustment.new(123, 1, 200, 1, 10, 0)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM