1

I am trying to add a new Frame every time the user clicks "new" on the menu,but I can't seem to somehow import the core program to add the Frame to the notebook. This is my debugging error:

Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Python27\lib\lib-tk\Tkinter.py", line 1470, in __call__
return self.func(*args)
File "C:\Users\Owner\Desktop\HTML IDE\UI_functions.py", line 8, in UI_add_frame
UI_notebook.add(UI_f2,text = "test")
NameError: global name 'UI_notebook' is not defined?

Here is my code:

import Tkinter
from Tkinter import *
import ttk
import UI_functions

root = Tkinter.Tk()
root.title("HTML IDE coded in Python")
w = 515
h = 590
sw = root.winfo_screenwidth()
sh = root.winfo_screenheight()
x = (sw - w)/2
y = (sh - h)/2
root.geometry('%dx%d+%d+%d' % (w, h, x, y))

#Menu contents
UI_menu = Menu(root)
filemenu = Menu(UI_menu, tearoff=0)
filemenu.add_command(label="New",command = UI_functions.UI_add_frame)
filemenu.add_command(label="Open")
filemenu.add_command(label="Save")
filemenu.add_command(label="Save as...")
filemenu.add_command(label="Close")

filemenu.add_separator()

filemenu.add_command(label="Exit", command=root.quit)
UI_menu.add_cascade(label="File", menu=filemenu)
editmenu = Menu(UI_menu, tearoff=0)
editmenu.add_command(label="Undo")

editmenu.add_separator()

editmenu.add_command(label="Cut")
editmenu.add_command(label="Copy")
editmenu.add_command(label="Paste")
editmenu.add_command(label="Delete")
editmenu.add_command(label="Select All")

UI_menu.add_cascade(label="Edit", menu=editmenu)
helpmenu = Menu(UI_menu, tearoff=0)
helpmenu.add_command(label="Help Index")
helpmenu.add_command(label="About...")
UI_menu.add_cascade(label="Help", menu=helpmenu)

#Notebook contents
global UI_notebook
UI_notebook = ttk.Notebook(root)
UI_notebook.pack(fill="both", expand="yes")

#Nake frames for the notebook
UI_f1 = ttk.Frame()

#Add the frames to notebook
UI_notebook.add(UI_f1,text = "new")

root.config(menu=UI_menu)
root.mainloop()

That's the core of the program and this is the "UI_functions" module:

#Functions for the core program
import ttk
import Tkinter

#Add new notebook tab when user clicks "new"
def UI_add_frame():
   UI_f2 = ttk.Frame()
   UI_notebook.add(UI_f2,text = "test")
Peter Varo
  • 11,726
  • 7
  • 55
  • 77
Michelle Moya
  • 29
  • 1
  • 2
  • you need to import UI_notebook or wrap in inside a function. also please put indentation in your code – marcadian Nov 20 '13 at 02:48
  • possible duplicate of [Python: How to make a cross-module variable?](http://stackoverflow.com/questions/142545/python-how-to-make-a-cross-module-variable) Also check http://stackoverflow.com/questions/4976375/python-cross-module-variables – shad0w_wa1k3r Nov 20 '13 at 04:52

2 Answers2

1

In UI_functions.py you use element from core.py (UI_notebook) and in core.py you use element from UI_functions.py (UI_add_frame) so you have problem with "cross imports".

It is better to do this in that way:

import ttk
import Tkinter

def UI_add_frame(some_notebook):
    UI_f2 = ttk.Frame()
    some_notebook.add(UI_f2, text = "test")

and than you use lambda

filemenu.add_command(label="New",command = lambda:UI_functions.UI_add_frame(UI_notebook))

This way module don't use variable created dynamicly in program and so module is independent. You have not "cross imports"

furas
  • 134,197
  • 12
  • 106
  • 148
0

Change UI_functions.py to:

import ttk
import Tkinter
import mb # whatever filename you chose

def UI_add_frame():
    UI_f2 = ttk.Frame()
    mb.UI_notebook.add(UI_f2, text = "test")
furas
  • 134,197
  • 12
  • 106
  • 148
Charles Pehlivanian
  • 2,083
  • 17
  • 25