首页 > 解决方案 > 使用 GTK 在 GUI 中更新数据的功能建议

问题描述

我是使用 GTK 的新手,我正在尝试制作一个 GUI 来与 ROS 功能交互。为了测试我的设置,我必须能够显示和设置某些参数的当前值。

我似乎无法更新 GUI 中显示的值。

我已经完成了多个教程,并且已经有了带有网格和框的基本结构。我想要框中的树视图,这样我就可以向网格添加更多小部件(如按钮)以进一步扩展 GUI。

这是代码:

#!/usr/bin/env python

import sys
#PyGtk library import
import gi
gi.require_version("Gtk","3.0")
from gi.repository import Gtk
from gi.repository import GObject

#ROS related initializations
#import roslib; roslib.load_manifest('python_test')
import rospy
import os
import roslib; roslib.load_manifest('python_test')
from std_msgs.msg import String
from std_msgs.msg import Float32
Speed = 0

class MainWindow(Gtk.Window):
    def __init__(self):

        Populate = [("Speed", 0 , "M/S"),
            ("Force", 0, "N"),
            ("Get_Voltage", 0 , "V"),
            ("Set_Voltage", 0 , "V")]

        Gtk.Window.__init__(self, title="Button clicker 2.0")

        grid  = Gtk.Grid()
        self.add(grid)
        newpng=roslib.packages.get_pkg_dir('python_test')+"/src/axes.png"
        Layout = Gtk.Box()

        Populate_List_Store = Gtk.ListStore(str, float, str)
        for item in Populate:
            Populate_List_Store.append(list(item))

        #for row in Populate_List_Store:
            #rospy.loginfo(row[:])
            #rospy.loginfo(row[1])

        # tree view is the iteam that is displayed
        self.Populate_Tree_View = Gtk.TreeView(Populate_List_Store)

        for i, Col_title in enumerate(["Quantity", "Value", "Unit"]):

            # cell renderer Render means how to draw the data
            Renderer = Gtk.CellRendererText()
            #Create columns (text is column number)
            Column = Gtk.TreeViewColumn(Col_title, Renderer, text=i)

            # add Column to Treeview
        self.Populate_Tree_View.append_column(Column)
        Layout.pack_start(self.Populate_Tree_View, True, True, 0)
        grid.add(Layout)
            #Layout.pack_start(Populate_Tree_View, True, True, 0)

        #button
        button = Gtk.Button(label="Click here!")
        button.connect("clicked", self.button_clicked)
        grid.attach(button,1,0,2,1)

        #node init

        def Update_Speed(data):
            global Speed
            Speed = round(data.data,3)
            rospy.loginfo(Speed)

        def Update_Voltage_In(data):
            global In_Voltage
            In_Voltage = round(data.data,3)

        self.pub = rospy.Publisher('chatter', String, queue_size=10)
        rospy.init_node('talker')
        self.sub1 = rospy.Subscriber('/io_states_sub_plot_node/piston_sensor_average', Float32, Update_Speed)
        self.sub2 = rospy.Subscriber('/io_states_sub_plot_node/voltage_feed_back', Float32, Update_Voltage_In)

    def Update_Param(self):
        global Speed
        self.Populate_Tree_View[0][1] = Speed

    def button_clicked(self, widget):
        self.talker() #Calls talker function which sends a ROS message
        print ("You clicked the button")

    def talker(self):
            #ROS message hello world
        if not rospy.is_shutdown():
        str = "hello world %s"%rospy.get_time()
        rospy.loginfo(str)
        self.pub.publish(String(str))
    def Timer1_timeout(self):
        #Timer functions that sends ROS messages every second
        self.talker()
        return 1
    def MainWindow_destroy(self,widget):
        #MainWindow_destroy event
        sys.exit(0)

if __name__ == "__main__":
    #start the class
    window = MainWindow()
    window.connect("delete-event",Gtk.main_quit)
    GObject.timeout_add(1000, window.Update_Param) #Adds a timer to the GUI, with window.Timer1_timeout as a
    #callback function for the timer1
    window.show_all()
    Gtk.main()#Starts GTK

标签: pythongtk3ros

解决方案


我将数据添加到错误的结构中。如果您使用树视图中已经存在的数据,它显然不起作用

self.Populate_Tree_View[0][1] = Speed

如果您使用列表存储,它确实有效

Populate_List_Store[0][1] = Speed

感谢您的时间


推荐阅读