Skip to content

Merge formateli:onoffbutton into master to add OnOffButton #61

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/source/ttkwidgets/ttkwidgets.rst
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ ttkwidgets
DebugWindow
ItemsCanvas
LinkLabel
OnOffButton
ScaleEntry
ScrolledListbox
Table
Expand Down
10 changes: 10 additions & 0 deletions docs/source/ttkwidgets/ttkwidgets/ttkwidgets.OnOffButton.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
OnOffButton
===========

.. currentmodule:: ttkwidgets

.. autoclass:: OnOffButton
:show-inheritance:
:members:

.. automethod:: __init__
45 changes: 45 additions & 0 deletions examples/example_onoffbutton.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# -*- coding: utf-8 -*-

# Copyright (c) 2020 Fredy Ramirez <https://formateli.com>
# For license see LICENSE

from ttkwidgets import OnOffButton
try:
import Tkinter as tk
except ImportError:
import tkinter as tk
from tkinter import ttk


window = tk.Tk()


def grid_button(button, row, column):
window.columnconfigure(column, weight=1)
button.grid(row=row, column=column, padx=10, pady=10)


def print_value():
print('Button toggled')


onoff_off = OnOffButton(window, command=print_value)
grid_button(onoff_off, 0, 0)

onoff_off_dis = OnOffButton(window)
grid_button(onoff_off_dis, 0, 1)
onoff_off_dis['state'] = 'disabled'

onoff_on = OnOffButton(window, command=print_value, cursor='hand2')
grid_button(onoff_on, 0, 2)
onoff_on.set('on')

onoff_on_dis = OnOffButton(window)
grid_button(onoff_on_dis, 0, 3)
onoff_on_dis.set('on')
onoff_on_dis['state'] = 'disabled'

chk = ttk.Checkbutton(window)
grid_button(chk, 1, 0)

window.mainloop()
92 changes: 92 additions & 0 deletions tests/test_onoffbutton.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
# Copyright (c) 2020 Fredy Ramirez <https://formateli.com>
# For license see LICENSE
from ttkwidgets import OnOffButton
from tests import BaseWidgetTest
import tkinter as tk
from tkinter import ttk


class TestOnOffButton(BaseWidgetTest):
def test_onoffbutton_init(self):
onoff = OnOffButton(self.window)
onoff.pack()
self.window.update()

def test_onoffbutton_variable_default(self):
onoff = OnOffButton(self.window)
onoff.pack()
self.window.update()
variable = onoff._variable
self.assertEqual(isinstance(variable, tk.StringVar), True)
self.assertEqual(variable.get(), 'off')
variable.set('on')
self.assertEqual(variable.get(), 'on')

def test_onoffbutton_variable_int(self):
variable = tk.IntVar()
onoff = OnOffButton(self.window, variable=variable)
onoff.pack()
self.window.update()
self.assertEqual(variable.get(), 0)
variable.set(1)
self.assertEqual(variable.get(), 1)

def test_onoffbutton_variable_bool(self):
variable = tk.BooleanVar()
onoff = OnOffButton(self.window, variable=variable)
onoff.pack()
self.window.update()
self.assertEqual(variable.get(), False)
variable.set(True)
self.assertEqual(variable.get(), True)

def test_onoffbutton_variable_string(self):
variable = tk.StringVar()
onoff = OnOffButton(self.window, variable=variable)
onoff.pack()
self.window.update()
self.assertEqual(variable.get(), 'off')
variable.set('on')
self.assertEqual(variable.get(), 'on')

def test_onoffbutton_invoke(self):
onoff = OnOffButton(self.window, command=self._button_changed)
onoff.pack()
self.window.update()
self.assertEqual(onoff.get(), 'off')
result = onoff.invoke()
self.assertEqual('Invoked', result)
self.assertEqual(onoff.get(), 'on')

def test_onoffbutton_style(self):
style = ttk.Style()
onoff = OnOffButton(self.window)
onoff.pack()
self.window.update()
self.assertEqual(onoff.cget('class'), 'OnOffButton')
st = onoff.cget('style')
self.assertEqual(st, 'OnOffButton')

# New custom style passing at init
style.configure('OnOffButtonTest.OnOffButton',
background='orange', oncolor='brown')
onoff2 = OnOffButton(self.window,
style='OnOffButtonTest.OnOffButton')
onoff2.pack()
self.window.update()
self.assertEqual(onoff2.cget('class'), 'OnOffButton')
st = onoff2.cget('style')
self.assertEqual(st, 'OnOffButtonTest.OnOffButton')

# Passing style to widget config
style.configure('OnOffButtonTest2.OnOffButton',
background='purple', oncolor='blue')
onoff3 = OnOffButton(self.window)
onoff3.pack()
self.window.update()
onoff3.config(style='OnOffButtonTest2.OnOffButton')
st = onoff3.cget('style')
self.assertEqual(st, 'OnOffButtonTest2.OnOffButton')

def _button_changed(self):
return 'Invoked'
1 change: 1 addition & 0 deletions ttkwidgets/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@
from ttkwidgets.timeline import TimeLine
from ttkwidgets.tickscale import TickScale
from ttkwidgets.table import Table
from ttkwidgets.onoffbutton import OnOffButton
Loading