标题可能令人困惑,因为我不知道我在这里要做的事情(在Google上进行研究对我没有帮助的重要原因)。
基本上,我正在使用 Tkinter 在Splitbrains / pimenu中使用Python菜单。菜单很棒,这是所有菜单的基本概念。有一个menu.yaml.dist
文件,其中包含所有菜单选项Name
,Color
,Label
。然后,您将拥有Tkinter脚本,该脚本会调用menu.yaml.dist
文件来获取菜单项的名称,等等。当您单击菜单项时,它会运行一个子进程,该子进程会调用Shell运行脚本。这里出现的问题,无论我按哪个菜单图块都运行相同的脚本,我试图找出答案。我的目标是当我单击“ 5分钟”按钮运行带有5分钟计时器的程序,然后单击“ 2分钟”按钮运行带有2分钟计时器的程序。基本上我的问题是,无论我选择哪个菜单选项都运行相同的脚本,我希望每个按钮都具有自己的脚本。我希望你们能遵循这一点。我是16岁的,对Tkinter,Shells和Yaml来说还很陌生,所以这超出了我的脑海。
请以任何一种方向回应我可能会寻求解决方案(即使它只是正确的用语,这也将极大地帮助我的Google搜索)如果您想要截图,请随时给我dm(我不知道如果可以的话)或给我发电子邮件。 nexuscustoms15@aldenschools.org
def show_items(self, items, upper=None):
"""
Creates a new page on the stack, automatically adds a back button when there are
pages on the stack already
:param items: list the items to display
:param upper: list previous levels' ids
:return: None
"""
if upper is None:
upper = []
num = 0
# create a new frame
wrap = Frame(self, bg="black")
if len(self.framestack):
# when there were previous frames, hide the top one and add a back button for the new one
self.hide_top()
back = FlatButton(
wrap,
text='back…',
image=self.get_icon("arrow.left"),
command=self.go_back,
)
back.set_color("#FFD700") # yellow
back.grid(row=0, column=0, padx=1, pady=1, sticky=TkC.W + TkC.E + TkC.N + TkC.S)
num += 1
# add the new frame to the stack and display it
self.framestack.append(wrap)
self.show_top()
# calculate tile distribution
allitems = len(items) + num
rows = floor(sqrt(allitems))
cols = ceil(allitems / rows)
# make cells autoscale
for x in range(int(cols)):
wrap.columnconfigure(x, weight=1)
for y in range(int(rows)):
wrap.rowconfigure(y, weight=1)
# display all given buttons
for item in items:
act = upper + [item['name']]
if 'icon' in item:
image = self.get_icon(item['icon'])
else:
image = self.get_icon('scrabble.' + item['label'][0:1].lower())
btn = FlatButton(
wrap,
text=item['label'],
image=image
)
if 'items' in item:
# this is a deeper level
btn.configure(command=lambda act=act, item=item: self.show_items(item['items'], act),
text=item['label'] + '…')
btn.set_color("#2b5797") # dark-blue
else:
# this is an action
btn.configure(command=lambda act=act: self.go_action(act), )
if 'color' in item:
btn.set_color(item['color'])
# add buton to the grid
btn.grid(
row=int(floor(num / cols)),
column=int(num % cols),
padx=1,
pady=1,
sticky=TkC.W + TkC.E + TkC.N + TkC.S
)
num += 1
def get_icon(self, name):
"""
Loads the given icon and keeps a reference
:param name: string
:return:
"""
if name in self.icons:
return self.icons[name]
ico = self.path + '/ico/' + name + '.png'
if not os.path.isfile(ico):
ico = self.path + '/ico/' + name + '.gif'
if not os.path.isfile(ico):
ico = self.path + '/ico/cancel.gif'
self.icons[name] = PhotoImage(file=ico)
return self.icons[name]
def hide_top(self):
"""
hide the top page
:return:
"""
self.framestack[len(self.framestack) - 1].pack_forget()
def show_top(self):
"""
show the top page
:return:
"""
self.framestack[len(self.framestack) - 1].pack(fill=TkC.BOTH, expand=1)
def destroy_top(self):
"""
destroy the top page
:return:
"""
self.framestack[len(self.framestack) - 1].destroy()
self.framestack.pop()
def destroy_all(self):
"""
destroy all pages except the first aka. go back to start
:return:
"""
while len(self.framestack) > 1:
self.destroy_top()
def go_action(self, actions):
"""
execute the action script
:param actions:
:return:
"""
# hide the menu and show a delay screen
self.hide_top()
delay = Frame(self, bg="#2d89ef")
delay.pack(fill=TkC.BOTH, expand=1)
label = Label(delay, text="Writting Ticket", fg="white", bg="#2d89ef", font="Sans 30")
label.pack(fill=TkC.BOTH, expand=1)
self.parent.update()
# excute shell script
subprocess.call([self.path + '/hallpass.sh.dist'] + actions)
# remove delay screen and show menu again
delay.destroy()
self.destroy_all()
self.show_top()
基本上是在看Def go_action
,可能还想看Self.Show_items
,所以我想展示一些上下文。