Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

Embedding Python In Your C++ Application

, 22 May 2006 BSD
Wrapper classes to embed the Python interpreter in Standard C++ code
py_embed_res.zip
article_res
pyembed
README
test
README
gui_demo
Screenshot.png
README
from Tkinter import *

# The root window manager object
root = Tk()
root.title("New User")

# Memory attached to the three text input fields
sv_fname = StringVar(); #first name
sv_lname = StringVar(); #last name
sv_email = StringVar(); #email

# Data of a new user is returned to the C++ environment
# inside this dictionary. The pyembed C++ library
# converts this to a std::map<string, string>
data_map = {'FirstName' : '', 'LastName' : '', 'Email' : ''}

# turned on when ok button is clicked
ok_clicked = 0

# Callback to the "Ok" button
def ok():
    global ok_clicked
    data_map['FirstName'] = sv_fname.get();
    data_map['LastName'] = sv_lname.get();
    data_map['Email'] = sv_email.get();
    ok_clicked = 1
    root.destroy()

# Callback to the "Cancel" button
def cancel():
    global ok_clicked
    data_map['FirstName'] = ''
    data_map['LastName'] = ''
    data_map['Email'] = ''
    ok_clicked = 0
    root.destroy()

# Creates and displays the UI
# C++ application calls this function.
# Returns the dictionary of the newley create user
# or an empty dictionary of one item if the user
# clicked cancel
def show_win():    
    top = Frame(root)
    top.grid()
            
    fname_label = Label(top, width = 15, text = 'First Name:')
    fname_label.grid(row = 0)
    sv_fname.set('')
    fname_entry = Entry(top, width = 25, relief = 'groove',
                    textvariable = sv_fname)
    fname_entry.grid(row = 0, column = 1, sticky = W)

    lname_label = Label(top, width = 15, text = 'Last Name:')
    lname_label.grid(row = 1)
    sv_lname.set('')
    lname_entry = Entry(top, width = 25, relief = 'groove',
                    textvariable = sv_lname)
    lname_entry.grid(row = 1, column = 1, sticky = W)

    email_label = Label(top, width = 15, text = 'Email:')
    email_label.grid(row = 2)
    sv_email.set('')
    email_entry = Entry(top, width = 40, relief = 'groove',
                    textvariable = sv_email)
    email_entry.grid(row = 2, column = 1, sticky = W)

    button_panel = Frame(top)
    button_panel.grid(row = 3, column = 1)
    ok_button = Button(button_panel, text = 'Ok', command = ok)
    ok_button.pack(side = 'left')
    cancel_button = Button(button_panel, text = 'Cancel', command = cancel)
    cancel_button.pack(side = 'left')
    
    root.mainloop()
    return get_data()

# Helper to show_ui()
# checks the state of ok_clicked and
# returns an appropriate dictionary
def get_data():
    if ok_clicked == 1:        
        return data_map
    else:        
        return {"" : ""}

    

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The BSD License

Share

About the Author

No Biography provided

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.141223.1 | Last Updated 23 May 2006
Article Copyright 2006 by AnOldGreenHorn
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid