我试图获取网格内的标签值,并且该网格在单击事件后位于按钮内。这是我的代码部分:for one_text in text_list: label_for_button = Gtk.Label(one_text) label_for_button.set_line_wrap(True) image_for_button = Gtk.Image.new_from_file("img.png") grid_in_button = Gtk.Grid() grid_in_button.add(image_button) grid_in_button.attach_next_to(label_for_button, image_for_button, Gtk.PositionType.BOTTOM, 1, 2) grid_in_button.show_all() button.add(grid_in_button) button.connect("clicked", self.on_button_clicked)def on_button_clicked(self, widget): # here i wanna get the value of the label_for_button帮助..有什么想法吗?谢谢
1 回答
明月笑刀无情
TA贡献1828条经验 获得超4个赞
希望这段代码有帮助:
import gi
gi.require_version('Gtk','3.0')
from gi.repository import Gtk,GdkPixbuf
def btn_clicked(widget):
print(widget.get_label())
pixbuf = GdkPixbuf.Pixbuf.new_from_file_at_scale(filename="img.png", width=24, height=24, preserve_aspect_ratio=True)
img = Gtk.Image.new_from_pixbuf(pixbuf)
btn = Gtk.Button(label='some text',image=img,)
btn.connect('clicked',btn_clicked)
win = Gtk.Window()
win.connect("destroy", Gtk.main_quit)
win.add(btn)
win.show_all()
Gtk.main()
- 1 回答
- 0 关注
- 97 浏览
添加回答
举报
0/150
提交
取消