2013-12-18 68 views
0

我正在学习一个项目的GUI。我在GUI中的所有按钮都与我创建的函数绑定。这些函数调用已经预定义的函数。对于一些预定义的函数,我需要一个或两个参数,我已经用条目解决了这个问题。我在连接到特定按钮的正确条目中键入参数,当我按下按钮时,函数将与相应的参数一起运行。Tkinter,将函数保存到列表中,然后运行它们

我想要做的事情就是在某种程度上,当我按下一个按钮,该功能应该被保存到一个列表,而不是马上执行。当我按下“运行”按钮(我将创建一个新按钮)时,我的列表中的所有内容都将被执行。我一直在考虑使用列表框,但我不知道它们是如何工作的,或者它甚至有可能运行包含许多函数的列表框。有人对我有任何想法或解决方案吗?我可以使用此列表框还是有其他更好的使用方法?

class App: 
    def __init__(self, master): 
     frame = Frame(master) 
     frame.pack() 

     self.entry1 = IntVar() 
     self.entry2 = IntVar() 

     def do_something(): 
      value1 = self.entry1.get() 
      value2 = self.entry2.get() 
      self.listbox.insert(END, "predefined_function(value1, value2)") 

     def run_listbox_contents(): 
      pass 

     self.button = Button(frame, text="Move", command=lambda: do_something()) 
     self.button.pack(side=TOP) 

     self.entry1.set("value1") 
     self.entry = Entry(frame, textvariable=self.entry1) 
     self.entry.pack(side=TOP) 

     self.entry2.set("value2") 
     self.entry = Entry(frame, textvariable=self.entry2) 
     self.entry.pack(side=TOP) 

     self.listbox = Listbox(master) 
     self.listbox.pack(side=TOP) 


root = Tk() 

app = App(root) 

root.title("Mindstorms GUI") 
root.geometry("800x1200") 

root.mainloop() 
root.destroy() 

回答

0

如果我是你,我不想功能保存到列表中。我会为你提出另一种解决方案。

我想你已经听说过MVC(Model-View-Controller)的原理。在你的情况下,列表框是视图,而且节省了功能过程的一部分,然后调用他们一次是控制器的一部分。 将它们分开。

您可能想要保存并显示列表框中的任何字符串,以便让用户知道相应的功能已经登记并准备运行。例如,节省"Function1 aug1 aug2 aug3""Funtion2 aug1 aug2"或任何你喜欢为相应的功能的手柄。

和一个控制器部分,编写一个函数(假设conductor())。它读取把手从字符串列表,解析他们调用相应的功能。您想在哪里运行征募功能,您只需拨打conductor()即可。


更新:

由于您的评论我明白,你是相当新的编程。让我告诉你如何用给定的变量名写一个最简单的解析器。

def run_listbox(): 
    to_do_list = #get the list of strings 
    for handle_string in to_do_list: 
     #Let's say you got 
     #handle_string = "Predfined_function1 value1 value2" 
     #by here 
     handle = handle_string.split(" ") 
     #Split the string by space, so you got 
     #handle = ["Predfined_function1", "value1", "value2"] 
     #by here 
     if handle[0] == "Predfined_function1": 
      Predfined_function1(handle[1], handle[2]) #Call Predfined_function1(value1, value2) 
     elif handle[0] == "Predfined_function2": 
      Predfined_function2(handle[1], handle[2]) 
     #elif ... 
     #... 
     #elif ... 
     #... 
     #elif ... 
     #... 

这是不是一个完美的解析器,但我希望它可以让你知道什么对解析器的样子。

+0

因为我对编程完全陌生,所以MVC不是我之前听说过的。但我想我明白这一点,你的意思。假设我将“Predfined_function value1 value 2”保存为列表框的一个字符串,我将使用函数run_listbox()。我有什么要写,以便该函数解析所有我的字符串,然后调用它们(在这种情况下,Predfined_function(value1,value2)?由于我新编程,我不认为我遇到了解析尚未 – user3067156

+0

@ user3067156更新。 – Skyler

+0

如果我在列表框中有三个字符串,比如“Predefined_function1 value1 value2”,“Predfined_function2 value1”和“Predefined_function3(这个是确实的不需要参数)。我必须写什么才能让列表框中的每个字符串都被称为handle_string?是否只写了handle_string =“”(通常是一个字符串),还是我在另一种方式?希望你明白我在问什么问题 – user3067156

1

只需使用标准列表即可。

像这样

def hest(txt): 
    print "hest: " +txt 

def horse(txt): 
    print "horse: " + txt 

funcList = [] 

funcList.append(hest)  
funcList.append(horse)  

for x in funcList: 
    x("Wow") 

此输出

hest: Wow 
horse: Wow 

了这你想要什么?

相关问题