1# python/example2.py -- Python version of an example application that shows 2# how to use the form helper class. For a C++ implementation, see 3# '../src/example2.cpp'. 4# 5# NanoGUI was developed by Wenzel Jakob <wenzel@inf.ethz.ch>. 6# The widget drawing code is based on the NanoVG demo application 7# by Mikko Mononen. 8# 9# All rights reserved. Use of this source code is governed by a 10# BSD-style license that can be found in the LICENSE.txt file. 11 12import nanogui 13import math 14import gc 15 16from nanogui import Screen, FormHelper 17 18bvar = True 19ivar = 12345678 20dvar = math.pi 21strvar = "A string" 22enumvar = 1 23colvar = nanogui.Color(.5, .5, .7, 1) 24 25 26def make_accessors(name): 27 def setter(value): 28 globals()[name] = value 29 30 def getter(): 31 return globals()[name] 32 return setter, getter 33 34nanogui.init() 35 36use_gl_4_1 = False # Set to True to create an OpenGL 4.1 context. 37if use_gl_4_1: 38 # NanoGUI presents many options for you to utilize at your discretion. 39 # See include/nanogui/screen.h for what all of the options are. 40 screen = Screen((500, 700), "NanoGUI test [GL 4.1]", glMajor=4, glMinor=1) 41else: 42 screen = Screen((500, 700), "NanoGUI test") 43 44gui = FormHelper(screen) 45window = gui.addWindow((10, 10), "Form helper example") 46 47gui.addGroup("Basic types") 48gui.addBoolVariable("bool", *make_accessors("bvar")) 49gui.addStringVariable("string", *make_accessors("strvar")) 50 51gui.addGroup("Validating fields") 52gui.addIntVariable("int", *make_accessors("ivar")) 53gui.addDoubleVariable("double", *make_accessors("dvar")) 54 55gui.addGroup("Complex types") 56gui.addEnumVariable("Enumeration", *make_accessors("enumvar")) \ 57 .setItems(["Item 1", "Item 2", "Item 3"]) 58 59def cp_final_cb(color): 60 print( 61 "ColorPicker Final Callback: [{0}, {1}, {2}, {3}]".format(color.r, 62 color.g, 63 color.b, 64 color.w) 65 ) 66 67 68gui.addColorVariable("Color", *make_accessors("colvar")).setFinalCallback(cp_final_cb) 69 70gui.addGroup("Other widgets") 71 72 73def cb(): 74 print("Button pressed.") 75gui.addButton("A button", cb) 76 77screen.setVisible(True) 78screen.performLayout() 79window.center() 80 81nanogui.mainloop() 82screen = gui = window = None 83gc.collect() 84nanogui.shutdown() 85