Reputation: 599
I am using python 3.4.2, windows 8.1 and I have a GUI "reload 0.py" that sums two values when I press a button, and shows the result in a box. I have another GUI "reload1.py" with a button. I want to open and "reload0.py" when I press the button in "reload1.py".
I am using this in the button
exec(open("C:\\Users\\me\\Desktop\\rel\\reload0.py").read())
but I am getting the following error
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Python34\lib\idlelib\run.py", line 121, in main
seq, request = rpc.request_queue.get(block=True, timeout=0.05)
File "C:\Python34\lib\queue.py", line 175, in get
raise Empty
queue.Empty
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Python34\lib\tkinter\__init__.py", line 1533, in __call__
return self.func(*args)
File "<string>", line 24, in mOp
NameError: name 'value1' is not defined
If I run "reload0.py" alone, it works, if I put the command in python shell, it works, but when I put the same command in the "reload1.py" button it does not work.
Upvotes: 0
Views: 2334
Reputation: 704
I've never seen anyone code callback functions and guis using exec so i cannot tell you why it is not working.
You should put the code in result0.py into a class or function, That way you can simply import the function/class and use it in a callback
Let us assume the code below is result0.py
import tkinter as tk
import tkinter.ttk as ttk
class SomeWindow(tk.Toplevel):
def __init__(self, parent):
tk.Toplevel.__init__(self, parent)
lab1 = ttk.Label(self, text='hihihi')
lab1.pack()
lab2 = tkt.Label(self, text='fdsfsfds')
lab2.pack()
Now in result1.py we import the SomeWindow and bind it to the callback
import tkinter as tk
import tkinter.ttk as ttk
from result0 import SomeWindow
root = tk.Tk()
but = ttk.Button(root, text='press me', command=lambda: SomeWindow(root))
but.pack()
tk.mainloop()
If you are just getting started with gui programming, you might want to take a look at the model view controller (MVC) design pattern
http://tkinter.unpythonic.net/wiki/ToyMVC
Some good tkinter references are:
http://effbot.org/tkinterbook/
http://infohost.nmt.edu/tcc/help/pubs/tkinter/web/index.html
https://docs.python.org/3/library/tkinter.html#module-tkinter
Also tkinter 8.6 was released in March 2015 but most documentation and tutorials for 8.5 still apply.
Upvotes: 2