从列表中进行选择以执行

时间:2014-09-13 07:30:29

标签: python

我有一个包含2个列表存储的脚本(每个存储有3个选项),并希望执行给定的选择。

from gi.repository import Gtk
import subprocess
import threading

class GridWindow(Gtk.Window):

    def __init__(self):
        Gtk.Window.__init__(self, title="Grid Example")

        grid = Gtk.Grid()
        self.add(grid)

        scheduler_store = Gtk.ListStore(int, str)
    scheduler_store.append([1, "noop"])
    scheduler_store.append([2, "deadline"])
    scheduler_store.append([3, "cfq"])

        label1 = Gtk.Label("Chose the Kernel to install")
        label2 = Gtk.Label("Chose scheduler to patch")

        kernel_store = Gtk.ListStore(int, str)
        kernel_store.append([1, "3.16.1"])
        kernel_store.append([2, "3.16.0"])
        kernel_store.append([3, "3.15.9"])

        button1 = Gtk.Button(label="Firefox")
        button2 = Gtk.Button(label="Morse")
        button3 = Gtk.Button(label="Clipgrab")
        #button4 = Gtk.Button(label="Button 4")
        #button5 = Gtk.Button(label="Button 5")
        #button6 = Gtk.Button(label="Button 6")

    combobox1 = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=2)
        scheduler_combo = Gtk.ComboBox.new_with_model_and_entry(scheduler_store)
        #scheduler_combo.connect("changed", self.on_scheduler_combo_changed)
        scheduler_combo.set_entry_text_column(1)
        combobox1.pack_start(scheduler_combo, False, False, 0)

    combobox2 = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=2)
        kernel_combo = Gtk.ComboBox.new_with_model_and_entry(kernel_store)
        kernel_combo.connect("changed", self.on_kernel_combo_changed)
        kernel_combo.set_entry_text_column(1)
        combobox2.pack_start(kernel_combo, False, False, 0)

        grid.add(button1)
        grid.attach(button2, 0, 1, 1, 1)
        grid.attach_next_to(button3, button2, Gtk.PositionType.BOTTOM, 1, 1)
        grid.attach_next_to(label2, button2, Gtk.PositionType.RIGHT, 1, 1)
        grid.attach_next_to(combobox1, label2, Gtk.PositionType.BOTTOM, 1, 1)
        grid.attach_next_to(label1, label2, Gtk.PositionType. RIGHT, 1, 1)
        grid.attach_next_to(combobox2, label1, Gtk.PositionType.BOTTOM, 1, 1)

        #grid.attach_next_to(button4, button3, Gtk.PositionType.RIGHT, 2, 1)
        #grid.attach(button5, 1, 2, 1, 1)
        #grid.attach_next_to(button6, button5, Gtk.PositionType.RIGHT, 1, 1)

    def on_kernel_combo_changed(self,combo):
        tree_iter = combo.get_active_iter()
        if tree_iter != None:
            model = combo.get_model()
            row_id, name = model[tree_iter][:2]
            print("Selected: ID=%d, name=%s" % (row_id, name))
            if  == 1:
          def my_thread(obj):
        cmd = "apt-get install morse"
        proc = subprocess.Popen(cmd,shell=True, stdout=subprocess.PIPE)
        while True:
          line = proc.stdout.read(2)
          if not line:
            break
          threading.Thread(target=my_thread, args=(self,)).start()

        else:
            entry = combo.get_child()
            print("Entered: %s" % entry.get_text())

win = GridWindow()
win.connect("delete-event", Gtk.main_quit)
win.show_all()
Gtk.main()

我试过了:

if %d == 1:

哪个不起作用。

if kernel_store.append([1, "3.16.1"]) == 1 

也不起作用,

if Selected: ID=%d == 1:

也不起作用。

它应该读取用户选择的内容(它确实如此),但它应该启动一个函数。我不能实现后一部分。想法?

1 个答案:

答案 0 :(得分:0)

你没有在任何地方指定%d = ...(你不能,它不是一个有效的名字),那么为什么你希望if %d == 1:能够到达任何地方?

ListStore.append会返回TreeIter,因此if kernel_store.append([1, "3.16.1"]) == 1永远不会评估True

它开始对你的第三次尝试产生一些模糊的感觉,从表面上看它是最糟糕的:

if Selected: ID=%d == 1:

Selected永远不会被分配(尽管与%d不同,它可能是)而第二部分是SyntaxError,原因有三个,但看起来你似乎在尝试获取 %d令牌所占的值。这一行:

print("Selected: ID=%d, name=%s" % (row_id, name))

创建一个新字符串(例如"Selected: ID=42, name=Douglas"),将其传递给print函数,然后将其丢弃。该字符串未分配给任何名称,因此在此行之后无法访问。即使字符串是可访问的,例如:

s = "Selected: ID=%d, name=%s" % (row_id, name)
print(s)
您已经有权访问时,尝试从中提取值是没有意义的:

if row_id == 1: