python - 从 python Gtk 列表中选择一个选项并将其存储在变量中

标签 python python-3.x user-interface gtk pygtk

我正在使用 python Gtk 制作 GUI 菜单。 我制作了一个 Gtk 列表,并在 Gtk Assistant 窗口(有后退和下一个按钮)中描述它。 我想在按下下一个按钮后将列表中选定的选项存储在变量中,但我还没有找到方法。 下面的示例是关于 Gtk.Assistant 窗口及其首页。

import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
import os

IMAGE_FOLDER = "images/"
count = 1
image_list = []

while True:
    cur_image = os.popen("ls "+IMAGE_FOLDER+" | sed -n "+str(count)+"p | tr -d '\n'").read()
    if not cur_image:
         break
    image_list.append([cur_image])
    print("image list =", image_list)
    count+=1

class Assistant(Gtk.Assistant):
def __init__(self):
    Gtk.Assistant.__init__(self)
    self.set_title("Image Installer")
    self.set_default_size(400, 200)
    self.connect("cancel", self.on_cancel_clicked)
    self.connect("close", self.on_close_clicked)
    self.connect("apply", self.on_apply_clicked)

    self.image= Gtk.Image()
    self.image.set_from_file('./logo.png')

    box = Gtk.Box(orientation=Gtk.Orientation.VERTICAL)


    self.append_page(box)
    self.set_page_type(box, Gtk.AssistantPageType.INTRO)
    self.set_page_title(box, "Image selection")
    menu = Gtk.ListStore(str)
    for choice in range(len(image_list)):
        print(choice)

        menu.append(image_list[choice])
    menutreeview1 = Gtk.TreeView(menu)
    renderer1 = Gtk.CellRendererText()

    column1 = Gtk.TreeViewColumn("Select image for the installation", renderer1, text=0)
    menutreeview1.append_column(column1)

    box.pack_start(menutreeview1, True, True, 0)
    box.pack_start(self.image, True, True, 0)
    self.set_page_complete(box, True)

assistant = Assistant()
assistant.show_all()

Gtk.main()

最佳答案

您可以使用:

selectedtext = None
treeselection = self.menutreeview1.get_selection()
(treemodel, selectediter) = treeselection.get_selected()
if selectediter is not None:
  selectedtext = treemodel[selectediter][0]

记住将menutreeview1设为数据成员并将其选择模式设置为single:

self.menutreeview1 = Gtk.TreeView(menu)
self.menutreeview1.get_selection().set_mode(Gtk.SelectionMode.SINGLE)

关于python - 从 python Gtk 列表中选择一个选项并将其存储在变量中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59663140/

相关文章:

python - 将 CSV 数据读取为标题和值对

python - asyncio.loop.time() 是否与 datetime.datetime.now() 具有可比性?如何?

python - NavigationLayout Kivy err0r

android - 如何在 Canvas 上绘制大量矩形并且性能良好?

Java 类构造函数错误

Python有效地找到多个多项式的局部最大值/最小值

python - 如何使用 Pandas Dataframe 列解析和评估数学表达式?

python - 为什么将更多条件传递给此函数时出现错误

python - 如何计算递归调用次数?

ios - 对 didselectrowatindexpath 的按钮操作