使用GTK更新GUI中数据的函数建议

2024-05-08 23:40:10 发布

您现在位置:Python中文网/ 问答频道 /正文

我是新的工作与GTK和我试图使一个图形用户界面与ROS的功能。为了测试我的设置,我必须能够显示和设置某些参数的当前值。你知道吗

我似乎无法更新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

Tags: theimportselfgtkdatadefupdatebutton
1条回答
网友
1楼 · 发布于 2024-05-08 23:40:10

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

self.Populate_Tree_View[0][1] = Speed

如果使用liststore,它就可以工作

Populate_List_Store[0][1] = Speed

谢谢你抽出时间

相关问题 更多 >