我正在研究这个程序。我有两个问题。首先,在导入空数组之前或之后如何从数组中删除空值?其次,有没有更好的写方法。我知道我可以使用它进行更多的面向对象编程,但是坦率地说,我现在确实让我感到困惑。
######imports python tools need for ui and csv reading
from tkinter import *
import tkinter as tk
import csv
#####establishes arrays that the lables and scripst are saved in
smename = []
smescript = []
rname = []
rscript = []
acname = []
acscript = []
tsname = []
tsscript = []
#####pulls infor from csv and imports to arrays
for d in csv.DictReader(open('script.csv')):
smename.append((d['smename']))
smescript.append((d['smescript']))
rname.append((d['rname']))
rscript.append((d['rscript']))
acname.append((d['acname']))
acscript.append((d['acscript']))
tsname.append((d['tsname']))
tsscript.append((d['tsscript']))
#####functions for all the option boxes to pull the needed scripts based on the button lable
def index(*entry):
dex = smename.index(*entry)
r = Tk()
r.withdraw()
r.clipboard_clear()
r.clipboard_append(smescript[dex])
r.update()
r.destroy()
tkvar1.set('Start, Middle, & End')
def rindex(*entry):
dex = rname.index(*entry)
r = Tk()
r.withdraw()
r.clipboard_clear()
r.clipboard_append(rscript[dex])
r.update()
r.destroy()
tkvar2.set('Remote')
def acindex(*entry):
dex = acname.index(*entry)
r = Tk()
r.withdraw()
r.clipboard_clear()
r.clipboard_append(acscript[dex])
r.update()
r.destroy()
tkvar3.set('Abandoned Chat?')
def tsindex(*entry):
dex = tsname.index(*entry)
r = Tk()
r.withdraw()
r.clipboard_clear()
r.clpboard_append(tsscript[dex])
r.update()
r.destroy()
tkvar4.set('Troubleshooting Steps')
#####establishes ui infromation and features
root = tk.Tk()
root.title('HD Tools')
root.attributes("-topmost", True)
frame = tk.Frame(root)
frame.pack(pady = 5, padx = 5)
####simple option box
tkvar1 = StringVar(root)
popupMenu = OptionMenu(frame, tkvar1, *smename, command=index)
popupMenu.configure(width=20)
popupMenu.grid(row = 1, column = 1)
tkvar1.set('Start, Middle, & End')
tkvar2 = StringVar(root)
popupMenu = OptionMenu(frame, tkvar2, *rname, command=rindex)
popupMenu.configure(width=20)
popupMenu.grid(row = 2, column = 1)
tkvar2.set('Remote')
tkvar3 = StringVar(root)
popupMenu = OptionMenu(frame, tkvar3, *acname, command=acindex)
popupMenu.configure(width=20)
popupMenu.grid(row = 3, column = 1)
tkvar3.set('Abandoned Chat?')
tkvar4 = StringVar(root)
popupMenu = OptionMenu(frame, tkvar4, *tsname, command=tsindex)
popupMenu.configure(width=20)
popupMenu.grid(row = 4, column = 1)
tkvar4.set('Troubleshooting Steps')
#####runs the mainloop to start the ui =
root.mainloop()
答案 0 :(得分:1)
您可以使用None
从list
中删除filter
和false-y值:
>>> rg = [ 'hello', None, None, 'stack', '' ]
>>> rg = list(filter(None, rg))
>>> rg
[ 'hello', 'stack' ]
即使不使用面向对象的编程,最简单的方法就是保持代码干燥。这意味着,如果您发现自己一遍又一遍地使用相同的代码部分,请尝试使可重复使用的代码成为一个单独的函数。例如,
####simple option box
tkvar1 = StringVar(root)
popupMenu = OptionMenu(frame, tkvar1, *smename, command=index)
popupMenu.configure(width=20)
popupMenu.grid(row = 1, column = 1)
tkvar1.set('Start, Middle, & End')
tkvar2 = StringVar(root)
popupMenu = OptionMenu(frame, tkvar2, *rname, command=rindex)
popupMenu.configure(width=20)
popupMenu.grid(row = 2, column = 1)
tkvar2.set('Remote')
这两个块基本上相同,除了一些小的差异。您可以将它们组合成一个可重用的功能:
def add_grid(data, command, title, row):
var = StringVar(root)
popupMenu = OptionMenu(frame, var, *data, command=command)
popupMenu.configure(width=20)
popupMenu.grid(row = row, column = 1)
var.set(title)
add_grid(smename, index, 'Start, Middle, & End', 1)
add_grid(rname, rindex, 'Remote', 2)