0

I have created a GUI script, the goal is after pressing the 'Run' button the chosen function(defined in another script and is imported at the beginning) in the Label widget will be executed.

My question is: How can the variable in the chosen function be changed(eg. the value N and it'll be saved) without restarting this GUI application and after pressing 'Run' the change can be dynamically realized.

Below shows the script:

# -*- coding:utf-8 -*-

from Tkinter import *
from function import func_test

class App:

    def __init__(self):

        self.root = Tk()
        self.root.geometry('200x80')
        self.root.title('Test')

        self.function = {'function': func_test}
        self.func = StringVar()

        self.lb = Listbox(self.root, selectmode='browse', height=2)
        self.lb.bind('<ButtonRelease-1>', self.get_function)

        for item in self.function.keys():
            self.lb.insert(END, item)

        self.lb.pack()

        Button(self.root, text='Run', command=self.run).pack()

    def get_function(self, event):
        self.func.set('')
        tmp = self.lb.get(self.lb.curselection())
        self.func.set(tmp)

    def run(self):
        func = self.func.get()
        function = self.function[func]
        function(3, 4)

if __name__ == '__main__':
    gui = App()
    mainloop()

And this is the function, in which value N should be changed:

def func_test(a, b):
    N = 2  # this variable can be changed and saved duing the GUI is running
    res = a + b * N
    print res

Any help would be very much appreciated.

  • Something like this maybe? http://stackoverflow.com/questions/437589/how-do-i-unload-reload-a-python-module – Lafexlos Nov 28 '16 at 07:32
  • Yeah, I think `reload` is helpful, but I don't know how to reload the `func_test`, when I added `del(func_test), reload(func_test)`, the console prints `global name 'func_test' is not defined`. Could you explain how to change the script? Thanks very much indeed! – yuhou CHEN Nov 28 '16 at 09:46
  • 2
    I have already solved my problem after learning this [http://stackoverflow.com/questions/7271082/how-to-reload-a-modules-function-in-python], anyway thank you a lot! – yuhou CHEN Nov 28 '16 at 15:34

0 Answers0