所以,我正在做一个项目,这需要我找到一个列表中有多少“对象”,这是我通过 完成的len()
,但是,现在我需要在choicebox()
每个条目都有选择的地方(在 easygui 中)在该列表中,而不会引发“超出范围”异常。
基本上,如果列表中有 3 个条目,那么我需要choicebox(msg="",title="",choices=[e[1])
成为choicebox(msg="",title="",choices=[e[1],e[2],e[3]])
,如果有 5 个选项,我需要成为choicebox(msg="",title="",choices=[e[1],e[2],e[3],e[4],e[5]])
等等。
注意:我需要e[0]
跳过,即.DS_Store
,desktop.ini
或thumbs.db
.
我在那之前列出了目录,所以如果你能告诉我如何只使目录最终出现在列表中,或者甚至如何将条目限制为 22 个,那也将不胜感激!
对不起这个noobish问题!我想不出如何搜索这样的东西,或者一个合适的标题......
编辑:由于请求,这是我的脚本。它几乎没有错误,但非常不完整和损坏;
#imports
from easygui import *
import os
#variables
storyname = None
#get user action
def selectaction():
d = str(buttonbox(msg="What would you to do?",title="Please Select an Action.",choices=["View Program Info","Start Reading!","Exit"]))
if d == "View Program Info":
msgbox(msg="This program was made solely by Thecheater887. Program Version 1.0.0. Many thanks to the following Story Authors; Thecheater887 (Cheet)",title="About",ok_button="Oh.")
selectaction()
elif d == "Exit":
exit
else:
enterage()
#get reader age
def enterage():
c = os.getcwd()
# print c
b = str(enterbox(msg="Please enter your age",title="Please enter your age",default="Age",strip=True))
# print str(b)
if b == "None":
exit()
elif b == "Age":
msgbox(msg="No. Enter your age. Not 'Age'...",title="Let's try that again...",ok_button="Fine...")
enterage()
elif b == "13":
# print "13"
choosetk()
elif b >= "100":
msgbox(msg="Please enter a valid age between 0 and 100.",title="Invalid Age!")
enterage()
elif b >= "14":
# print ">12"
choosema()
elif b <= "12":
# print "<12"
choosek()
else:
fatalerror()
#choose a kids' story
def choosek():
os.chdir("./Desktop/Stories/Kid")
f = str(os.getlogin())
g = "/Users/"
h = "/Desktop/Stories/Kid"
i = g+f+h
e = os.listdir(i)
names = [name for name in e if name not in ('.DS_Store', 'desktop.ini', 'thumbs.db')]
limit = 22 # maximum entries in the choicebox --> e[1] until e[22]
for i in xrange(1, len(e)): # starting from 1 because you don't want e[0] in there
if(i > limit):
break # so if you have 100 files, it will only list the first 22
else:
names.append(e[i])
#names = e[1:23]
choicebox(msg="Please select a story.",title="Please Select a Story",choices=names)
#choose a mature story
def choosema():
os.chdir("./Desktop/Stories/Mature")
#choose a teen's story
def choosetk():
os.chdir("./Desktop/Stories/Teen")
def fatalerror():
msgbox(msg="A fatal error has occured. The program must now exit.",title="Fatal Error!",ok_button="Terminate Program")
#select a kids' story
def noneavailable():
msgbox(msg="No stories are available at this time. Please check back later!",title="No Stories Available",ok_button="Return to Menu")
enterage()
selectaction()