2017-05-09 71 views
1

我有一個Excel電子表格,我正在從中提取信息。這可能會在某個時候變成數據庫,但現在可以用於測試。它與數據兩列,看起來像:從字典列表中選擇Python Tkinter單選按鈕

Key | ProgramName |  Path 

0  | Calculator  |  C:\Windows\System32\calc.exe 
1  | Notepad  |  C:\Windows\System32\notepad.exe 

我已經建立了使用Python Tkinter的一個非常基本的圖形用戶界面,並使用xlrd庫從電子表格中的數據提取到一個包含字典條目列表。
https://pypi.python.org/pypi/xlrd

這給我的字典項的列表:

[{'Path':'C:\Windows\System32\notepad.exe', 'Program':'Notepad', 'Key':0.0}, {'Path':'C:\Windows\System32\calc.exe', 'Program':'Calculator', 'Key':1.0}] 

我可以得到單選按鈕體面的工作,但我有一個問題,拉路徑數據,並重新使用它,因爲我將(希望)使用subprocess.Popen實際打開選定的程序。

繼承人的代碼至今:

from Tkinter import * 
from xlrd import open_workbook 
import os 
import subprocess 

class App: 

def __init__(self, master): 

    frame = Frame(master) 
    frame.pack() 

    bottomframe= Frame(master) 
    bottomframe.pack(side = BOTTOM) 

    book = open_workbook('programlist.xls') 
    sheet = book.sheet_by_index(0) 

    # read header values into the list  
    keys = [sheet.cell(0, col_index).value for col_index in xrange(sheet.ncols)] 

    global dict_list 
    dict_list = [] 
    for row_index in xrange(1, sheet.nrows): 
     d = {keys[col_index]: sheet.cell(row_index, col_index).value 
      for col_index in xrange(sheet.ncols)} 
     dict_list.append(d) 

    self.w = StringVar() 
    self.w.set(dict_list[0]['Key']) # initialize 

    for eachprogram in dict_list: 
     self.c = Radiobutton(master, text=eachprogram['Program'], variable=self.w, value=eachprogram['Key']) 
     self.c.pack(anchor=W) 


    self.quitButton = Button(
     bottomframe, text="QUIT" , fg="red", command=frame.quit 
     ) 
    self.quitButton.pack(side=LEFT, anchor=S) 


    self.programRun = Button(bottomframe, text="Run", command=self.programRun) 
    self.programRun.pack(side=LEFT, anchor=S) 


def programRun(self): 
    ??? 

root = Tk() 

app = App(root) 

root.mainloop() 
root.destroy() 

不知道我需要做的,這樣當按下programRun按鈕時,正確的路徑被拉到這樣我就可以把它變成一個「subprocess.Popen」命令。 我是否需要創建另一個變量?我可以使用字典鍵來獲取這些信息嗎?

任何建議,非常感謝。

+0

檢查您的格式。它看起來像'programRun'應該縮進。 –

回答

1

從示例中拉出路徑並不是一件難事,這裏甚至有兩個選項可供選擇。而且你不需要創建另一個變量。

根據Docs

可變選項必須被設置爲控制變量,無論是一個IntVar或STRINGVAR。功能組中的所有單選按鈕必須共享相同的控制變量。

將組中每個單選按鈕的值選項設置爲不同的值。無論用戶何時設置單選按鈕,該變量都將設置爲該單選按鈕的值選項,並且共享該組的所有其他單選按鈕將被清除。

正如你看到的 - 你不需要使用StringVar,但DoubleVar因爲你的value參數(和密鑰)是floats。缺點 - 你需要遍歷列表來檢查Key的每個字典。

try: 
    import tkinter as tk 
except ImportError: 
    import Tkinter as tk 


class App(tk.Tk): 
    def __init__(self, *args, **kwargs): 
     tk.Tk.__init__(self, *args, **kwargs) 

     self.frame = tk.Frame(self) 
     self.frame.pack() 

     self.bottomframe = tk.Frame(self) 
     self.bottomframe.pack(side=tk.BOTTOM) 

     # book = open_workbook('programlist.xls') 
     # sheet = book.sheet_by_index(0) 

     # read header values into the list 
     # keys = [sheet.cell(0, col_index).value for col_index in xrange(sheet.ncols)] 

     self.keys = ['Key', 'ProgramName', 'Path'] 
     self.dict_list = [{'Path': r'C:\Windows\System32\notepad.exe', 'Program': 'Notepad', 'Key': 0.0}, 
          {'Path': r'C:\Windows\System32\calc.exe', 'Program': 'Calculator', 'Key': 1.0}] 

     # global dict_list 
     # dict_list = [] 
     # for row_index in xrange(1, sheet.nrows): 
     # d = {keys[col_index]: sheet.cell(row_index, col_index).value 
     #  for col_index in xrange(sheet.ncols)} 
     # dict_list.append(d) 

     self.w = tk.DoubleVar() 
     self.w.set(self.dict_list[0]['Key']) # initialize 

     for each_program in self.dict_list: 
      self.c = tk.Radiobutton(self.master, text=each_program['Program'], variable=self.w, value=each_program['Key']) 
      self.c.pack(anchor=tk.W) 


     self.quitButton = tk.Button(
      self.bottomframe, text="QUIT", fg="red", command=self.frame.quit 
      ) 
     self.quitButton.pack(side=tk.LEFT, anchor=tk.S) 


     self.programRun = tk.Button(self.bottomframe, text="Run", command=self.programRun) 
     self.programRun.pack(side=tk.LEFT, anchor=tk.S) 

    def programRun(self): 
     print('Pulled path: %s' % self.search_list_dict()) 

    def search_list_dict(self): 
     try: 
      return [item for item in self.dict_list if item['Key'] == self.w.get()][0]['Path'] 
     except IndexError: 
      return '' 

app = App() 
app.mainloop() 

作爲替代方案 - 您可以使用一個StringVarPath項目作爲value參數!只需更改一些行:

try: 
    import tkinter as tk 
except ImportError: 
    import Tkinter as tk 


class App(tk.Tk): 
    def __init__(self, *args, **kwargs): 
     tk.Tk.__init__(self, *args, **kwargs) 
     ... 
     self.w = tk.StringVar() 
     self.w.set(self.dict_list[0]['Path']) # initialize 

     for each_program in self.dict_list: 
      self.c = tk.Radiobutton(self.master, text=each_program['Program'], variable=self.w, value=each_program['Path']) 
      self.c.pack(anchor=tk.W) 
     ... 

    def programRun(self): 
     print('Pulled path: %s' % self.w.get()) 

app = App() 
app.mainloop() 
+0

TY的幫助!感謝這樣一個解釋良好的答覆! – Shiroslullaby