如何将列表变量设置为函数中生成的列表,并将列表框中的值传递给该函数

需要创建一个函数,该函数可以获取列表框中的用户选择,并使用这些选择来填充一个列表。

  • 希望能够在函数之外访问此列表,但不知道如何实现。
  • 知道应该调用该函数,但不知道如何将任何内容传递给它。
  1. 解决方案
    • 将列表变量 UsrFCList 添加到模块级命名空间。
    • usr_fc 函数中,使用 selctd_indiceslst_select 获取用户在列表框中的选择。
    • 遍历 lst_select 中的索引,并将列表框中相应位置的文本添加到 UsrFCList
    • 现在可以在函数之外访问任何放置在 UsrFCList 中的数据。
    • 也可以将 GUI 的内部数据、小部件和回调函数放入其自己的类中,以便它们可以共享类命名空间,并像 UsrFCList 的内容一样相互通信数据。
#import modules and functions
import arcpy
import tkFont
from Tkinter import *

#create entry widget and get/set variable/workspace with function 'set_wkspc'
def set_wkspc():
  wk = e_ws.get()
  arcpy.env.workspace = wk
  e_ws.quit()

wk_input = Tk()

e_ws = Entry(wk_input, width=75)
e_ws.pack()

e_ws.focus_set()

b = Button(wk_input, text="choose workspace", width=15, command=set_wkspc)
b.pack()

mainloop()

#function to list all feature classes within specified workspace
def getFC(ws):
  ws = arcpy.env.workspace
  FCList1 = []
  FCList2 = []
  fcs1 = arcpy.ListFeatureClasses()
  FCList1.append(fcs1)
  fds = arcpy.ListDatasets()
  for fd in fds:
    arcpy.env.workspace = ws + '/'  + fd
    fcs2 = arcpy.ListFeatureClasses()
    FCList2.append(fcs2)
  FCList1.extend(FCList2)
  return FCList1

x = arcpy.env.workspace

lb_list = getFC(x)

#new Listbox class and function that sets up a listbox size according to its contents
class AutoSzLB(Listbox):
def autowidth(self,maxwidth):
    f = tkFont.Font(font=self.cget("font"))
    pixels = 0
    for item in self.get(0, "end"):
        pixels = max(pixels, f.measure(item))
    # bump listbox size until all entries fit
    pixels = pixels + 10
    width = int(self.cget("width"))
    for w in range(0, maxwidth+1, 5):
        if self.winfo_reqwidth() >= pixels:
            break
        self.config(width=width+w)

#list variable for user's choice of feature classes and a function that creates
#list of chosen feature classes

UsrFCList = []  # Add this name to the module-level namespace.

def usr_fc(*events):
  selctd_indices = lbox.curselection()
  lst_select = list(selctd_indices)
  for i in lst_select:
    UsrFCList.append(lbox.get(i))  # The interpreter looks for UsrFCList in the local function namespace, and since you're not assigning to that name, it look at the next biggest namespace, which is the module namespace.
  lbox.quit()

#generate scrollbar, execute button and listbox of feature classes to select
#for analysis
fc_lb = Tk()
scrollbar = Scrollbar(fc_lb)
scrollbar.pack(side=RIGHT, fill=Y)
lbox = AutoSzLB(fc_lb,selectmode=EXTENDED)
for item in lb_list:
  lbox.insert(END, *item)

button = Button(fc_lb, text="Analyze selected feature classes", command=usr_fc)

lbox.autowidth(250)
lbox.pack()
button.pack()

#attach listbox to scrollbar
lbox.config(yscrollcommand=scrollbar.set)
scrollbar.config(command=lbox.yview)

mainloop()

#create entry widget and set workspace for analysis output
def set_out_wkspc():
  wk = out_ws.get()
  ident_ouput_path = wk
  out_ws.quit()

out_wk = Tk()

out_ws = Entry(out_wk, width=75)
out_ws.pack()

out_ws.focus_set()

b2 = Button(out_wk, text="set output workspace", width=20, command=set_wkspc)
b2.pack()

mainloop()
  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值