Skip to content

Commit

Permalink
Added helper method to avoid call get_variable for every variable. refs
Browse files Browse the repository at this point in the history
  • Loading branch information
alejandroautalan committed Sep 28, 2014
1 parent e7fbf82 commit 149724a
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 0 deletions.
5 changes: 5 additions & 0 deletions pygubu/builder/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,11 @@ def __find_image(self, relpath):
def get_variable(self, varname):
"""Return a tk variable created with 'create_variable' method."""
return self.tkvariables[varname]

def import_variables(self, container):
"""Helper method to avoid call get_variable for every variable."""
for keyword in self.tkvariables:
setattr(container, keyword, self.tkvariables[keyword])

def create_variable(self, varname, vtype=None):
"""Create a tk variable.
Expand Down
44 changes: 44 additions & 0 deletions tests/test_import_variables.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# encoding: utf8
import os
import sys
import unittest
try:
import tkinter as tk
import tkinter.ttk as ttk
except:
import Tkinter as tk
import ttk


pygubu_basedir = os.path.abspath(os.path.dirname(
os.path.dirname(os.path.realpath(sys.argv[0]))))
if pygubu_basedir not in sys.path:
sys.path.insert(0, pygubu_basedir)

import pygubu
import support


class MyContainer(object):
pass


class TestText(unittest.TestCase):

def setUp(self):
support.root_deiconify()
xmldata = 'test_import_variables.ui'
self.builder = builder = pygubu.Builder()
builder.add_from_file(xmldata)
self.widget = builder.get_object('mainwindow')

def tearDown(self):
support.root_withdraw()

def test_import_variables(self):
container = MyContainer()
self.builder.import_variables(container)
self.assertIsInstance(container.myvar_string, tk.StringVar)
self.assertIsInstance(container.myvar_int, tk.IntVar)
self.assertIsInstance(container.myvar_double, tk.DoubleVar)
self.assertIsInstance(container.myvar_boolean, tk.BooleanVar)
52 changes: 52 additions & 0 deletions tests/test_import_variables.ui
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?xml version='1.0' encoding='utf-8'?>
<interface>
<object class="ttk.Frame" id="mainwindow">
<property name="height">200</property>
<property name="width">200</property>
<layout>
<property name="column">0</property>
<property name="propagate">True</property>
<property name="row">0</property>
</layout>
<child>
<object class="ttk.Entry" id="Entry_1">
<property name="textvariable">string:myvar_string</property>
<layout>
<property name="column">0</property>
<property name="propagate">True</property>
<property name="row">0</property>
</layout>
</object>
</child>
<child>
<object class="ttk.Entry" id="Entry_2">
<property name="textvariable">int:myvar_int</property>
<layout>
<property name="column">0</property>
<property name="propagate">True</property>
<property name="row">1</property>
</layout>
</object>
</child>
<child>
<object class="ttk.Entry" id="Entry_3">
<property name="textvariable">double:myvar_double</property>
<layout>
<property name="column">0</property>
<property name="propagate">True</property>
<property name="row">2</property>
</layout>
</object>
</child>
<child>
<object class="ttk.Entry" id="Entry_4">
<property name="textvariable">boolean:myvar_boolean</property>
<layout>
<property name="column">0</property>
<property name="propagate">True</property>
<property name="row">3</property>
</layout>
</object>
</child>
</object>
</interface>

0 comments on commit 149724a

Please sign in to comment.