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.