Python 关于使用GTK更新GUI中数据的函数的建议
我不熟悉GTK,我正在尝试制作一个GUI来与ROS功能接口。为了测试我的设置,我必须能够显示和设置某些参数的当前值 我似乎无法更新GUI中显示的值 我已经完成了多个教程,并且已经有了我的基本结构,包括网格和长方体。我希望树视图在框中,这样我就可以在网格中添加更多的小部件,如按钮,以进一步扩展GUI 代码如下:Python 关于使用GTK更新GUI中数据的函数的建议,python,gtk3,ros,Python,Gtk3,Ros,我不熟悉GTK,我正在尝试制作一个GUI来与ROS功能接口。为了测试我的设置,我必须能够显示和设置某些参数的当前值 我似乎无法更新GUI中显示的值 我已经完成了多个教程,并且已经有了我的基本结构,包括网格和长方体。我希望树视图在框中,这样我就可以在网格中添加更多的小部件,如按钮,以进一步扩展GUI 代码如下: #!/usr/bin/env python import sys #PyGtk library import import gi gi.require_version("Gtk","3.
#!/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
我将数据添加到错误的结构中。如果您使用的数据已经在树视图中,它显然不起作用
self.Populate_Tree_View[0][1] = Speed
如果您使用liststore,它确实可以工作
Populate_List_Store[0][1] = Speed
感谢您的时间如果您在GTK小部件中添加值,那么它应该在不运行任何
update
功能的情况下显示它。您在def talker(self)中出现缩进错误: