1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
|
# encoding: utf-8
import os
import sys
import unittest
import tkinter as tk
import tkinter.ttk as ttk
import fixpath
import pygubu
import support
class TestButton(unittest.TestCase):
def setUp(self):
support.root_deiconify()
xmldata = """<?xml version="1.0" ?>
<interface>
<object class="ttk.Frame" id="mainwindow">
<property name="height">250</property>
<property name="width">250</property>
<layout>
<property name="column">0</property>
<property name="sticky">nsew</property>
<property name="propagate">True</property>
<property name="row">0</property>
</layout>
<child>
<object class="ttk.Button" id="testbutton">
<property name="class_">CustomButton</property>
<property name="command">on_button_click</property>
<property name="compound">right</property>
<property name="style">CustomButton.TButton</property>
<property name="text">Button Label</property>
<property name="textvariable">button_var</property>
<layout>
<property name="column">0</property>
<property name="propagate">True</property>
<property name="row">0</property>
</layout>
</object>
</child>
</object>
</interface>
"""
self.builder = builder = pygubu.Builder()
builder.add_from_string(xmldata)
self.widget = builder.get_object("testbutton")
self.is_style_setup = False
if self.is_style_setup:
self.is_style_setup = True
s = ttk.Style()
s.configure("CustomButton.TButton", color="Blue")
def tearDown(self):
support.root_withdraw()
def test_class(self):
self.assertIsInstance(self.widget, ttk.Button)
self.widget.destroy()
def test_class_(self):
tclobj = self.widget.cget("class")
class_ = str(tclobj)
self.assertEqual("CustomButton", class_)
self.widget.destroy()
def test_style(self):
tclobj = self.widget.cget("style")
style = str(tclobj)
self.assertEqual("CustomButton.TButton", style)
self.widget.destroy()
def test_command_dict(self):
success = []
def on_button_click():
success.append(1)
cbdic = {"on_button_click": on_button_click}
self.builder.connect_callbacks(cbdic)
self.widget.invoke()
self.assertTrue(success)
self.widget.destroy()
def test_command_self(self):
success = []
class AnObject:
def on_button_click(self):
success.append(1)
cbobj = AnObject()
self.builder.connect_callbacks(cbobj)
self.widget.invoke()
self.assertTrue(success)
self.widget.destroy()
def test_compound(self):
compound = str(self.widget.cget("compound"))
self.assertEqual("right", compound)
self.widget.destroy()
def test_btn_text(self):
txt = self.widget.cget("text")
self.assertEqual("Button Label", txt)
self.widget.destroy()
def test_btn_variable(self):
var = self.builder.get_variable("button_var")
self.assertIsInstance(var, tk.StringVar)
self.assertEqual("Button Label", var.get())
newlabel = "Label Changed"
var.set(newlabel)
self.assertEqual(newlabel, self.widget.cget("text"))
self.widget.destroy()
if __name__ == "__main__":
unittest.main()
|