Skip to content

Commit

Permalink
fix entry.py
Browse files Browse the repository at this point in the history
  • Loading branch information
rasql committed Jan 3, 2020
1 parent eb0094d commit d24186e
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 59 deletions.
Binary file modified docs/entry/entry.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
116 changes: 75 additions & 41 deletions docs/entry/entry.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,43 @@
# Entry widget for Entry, Combobox, Spinbox and Scale
from tklib import *


def cb(caller, event):
"""Callback function."""
print('item =', item)
print('value =', item.var.get())
print('caller =', caller)
print('value =', caller.var.get())
print('event =', event)


class EntryMixin:
"""Add callback function."""

def add_widget(self, label, widget, kwargs):
"""Add widget with optional label."""
if label == '':
super(widget, self).__init__(App.stack[-1], **kwargs)
self.grid()
else:
d = 2 if App.debug else 0
frame = ttk.Frame(App.stack[-1], relief='solid', borderwidth=d)
frame.grid(sticky='e')
ttk.Label(frame, text=label).grid()
super(widget, self).__init__(frame, **kwargs)
self.grid(row=0, column=1)

def add_cmd(self, cmd):
if isinstance(cmd, str):
self.cmd = cmd
cmd = self.cb
self.bind('<Return>', lambda event: cmd(self, event))

def cb(self, item=None, event=None):
"""Execute the cmd string in the widget context."""
exec(self.cmd)




class Entry(ttk.Entry, EntryMixin):
"""Create an Entry object with label and callback."""

Expand All @@ -31,16 +48,19 @@ def __init__(self, label='', cmd='', val='', **kwargs):
"""The arguments are differnt for Entry, Combobox, Spinbox, Slider.
Without label, grid() should have no arguments."""

if label == '':
super().__init__(App.stack[-1], textvariable=self.var, **kwargs)
self.grid()
else:
frame = ttk.Frame(App.stack[-1], relief='solid', borderwidth=2)
frame.grid(sticky='e')
ttk.Label(frame, text=label).grid()
super().__init__(frame, textvariable=self.var, **kwargs)
self.grid(row=0, column=1)

# if label == '':
# super().__init__(App.stack[-1], textvariable=self.var, **kwargs)
# self.grid()
# else:
# d = 2 if App.debug else 0
# frame = ttk.Frame(App.stack[-1], relief='solid', borderwidth=d)
# frame.grid(sticky='e')
# ttk.Label(frame, text=label).grid()
# super(Entry, self).__init__(frame, textvariable=self.var, **kwargs)
# self.grid(row=0, column=1)

self.add_widget(label, Entry, kwargs)
self['textvariable'] = self.var
self.add_cmd(cmd)


Expand All @@ -54,37 +74,47 @@ def __init__(self, label='', values='', cmd='', val=0, **kwargs):
self.var = tk.StringVar()
self.var.set(values[val])

if label == '':
super().__init__(
App.stack[-1], textvariable=self.var, values=values, **kwargs)
self.grid()
else:
frame = ttk.Frame(App.stack[-1], relief='solid', borderwidth=2)
frame.grid(sticky='e')
ttk.Label(frame, text=label).grid()
super().__init__(frame, textvariable=self.var, values=values, **kwargs)
self.grid(row=0, column=1)

# if label == '':
# super().__init__(
# App.stack[-1], textvariable=self.var, values=values, **kwargs)
# self.grid()
# else:
# d = 2 if App.debug else 0
# frame = ttk.Frame(App.stack[-1], relief='solid', borderwidth=d)
# frame.grid(sticky='e')
# ttk.Label(frame, text=label).grid()
# super().__init__(frame, textvariable=self.var, values=values, **kwargs)
# self.grid(row=0, column=1)

self.add_widget(label, Combobox, kwargs)
self['textvariable'] = self.var
self['values'] = values
self.add_cmd(cmd)
self.bind('<<ComboboxSelected>>', self.cb)


class Spinbox(ttk.Spinbox, EntryMixin):
"""Create a Spinbox with label and callback."""

def __init__(self, label='', cmd='', val=0, **kwargs):
def __init__(self, label='', cmd='', values='', val=0, **kwargs):

if isinstance(values, str):
values = values.split(';')
if len(values) > 1:
val = values[val]

self.var = tk.StringVar(value=val)

if label == '':
super().__init__(
App.stack[-1], textvariable=self.var, **kwargs)
App.stack[-1], textvariable=self.var, values=values, **kwargs)
self.grid()
else:
frame = ttk.Frame(App.stack[-1], relief='solid', borderwidth=2)
d = 2 if App.debug else 0
frame = ttk.Frame(App.stack[-1], relief='solid', borderwidth=d)
frame.grid(sticky='e')
ttk.Label(frame, text=label).grid()
super().__init__(frame, textvariable=self.var, **kwargs)
super().__init__(frame, textvariable=self.var, values=values, **kwargs)
self.grid(row=0, column=1)

self.add_cmd(cmd)
Expand All @@ -97,12 +127,16 @@ def __init__(self, label='', cmd='', val=0, **kwargs):

self.var = tk.IntVar(value=val)

if not 'length' in kwargs:
kwargs.update({'length': 200})

if label == '':
super().__init__(
App.stack[-1], variable=self.var, **kwargs)
self.grid()
else:
frame = ttk.Frame(App.stack[-1], relief='solid', borderwidth=2)
d = 2 if App.debug else 0
frame = ttk.Frame(App.stack[-1], relief='solid', borderwidth=d)
frame.grid(sticky='e')
ttk.Label(frame, text=label).grid()
super().__init__(frame, variable=self.var, **kwargs)
Expand All @@ -116,16 +150,16 @@ def __init__(self, label='', cmd='', val=0, **kwargs):


if __name__ == '__main__':
app = App('Entry widgets')
Entry('Call cb', cb)
Entry('Name', 'print(self.var.get())')
Entry('', 'print(self.var.get())', val='inital value')
Entry('Password', 'print(self.var.get())', show='*')
Entry('', 'print(self.var.get())', show='*', val='inital')
Combobox('Language', 'French;German;English', 'print(self.var.get())')
Combobox('', 'French;German;English', 'print(self.var.get())', val=2)
Spinbox('Label', to=10, )
Spinbox('Label', 'print(self.var.get())', to=10, width=10)
Scale('Scale', 'print(self.var.get())', to=10)
Scale(cmd='print(self.var.get())', to=100)
app = App('Entry widgets', True)

Entry('Entry', 'print(self.var.get())')
Entry('', 'print(self.var.get())', val='inital value', width=10)
Combobox('Combobox', 'Item 1;Item 2;Item 3', 'print(self.var.get())')
Combobox('', 'Item 1;Item 2;Item 3',
'print(self.var.get())', val=2, width=10)
Spinbox('Spinbox', to=10, val=5)
Spinbox('', 'print(self.var.get())', to=10, width=10)
Scale('Scale', 'print(self.var.get())', to=10, val=5)
Scale(cmd='print(self.var.get())', to=100, length=100)

app.run()
12 changes: 9 additions & 3 deletions docs/entry/entry.rst
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
Entry, Combox, Spinbox
=========================

Several widgets allow to enter text or numerical information:
Several widgets allow to enter text or numerical information in a single field.

* the **Entry** widgets allows to type text
* the **Combobox** allows to either type text or select from a list
* the **Spinbox** allows to select from a range
* the **Combobox** allows to either type text or select text from a drop-down list
* the **Spinbox** allows to select from a numerical range or from a list
* the **Scale** allows to use a slider to choose a value

.. image:: entry0.png

:download:`entry0.py<entry0.py>`

Since these widgets are very similar, we treat them in the same section.

Entry widget
------------

Expand Down
Binary file added docs/entry/entry0.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 10 additions & 0 deletions docs/entry/entry0.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from entry import *

app = App('Entry, Combobox, Spinbox')

Entry('Entry', val='enter text')
Combobox('Combobox', 'select item;item 2')
Spinbox('Spinbox', val=5, to=10)
Scale('Scale')

app.run()
1 change: 1 addition & 0 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ In this tutorial I will teach you to
radio/radio
check/check
entry/entry
grid/grid
basic/basic
concept/concept
event/event
Expand Down
17 changes: 2 additions & 15 deletions tklib.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,6 @@ def cb(self, event=None):
"""Execute the cmd string in the widget context."""
exec(self.cmd)

def add_label(self, w, label, **kwargs):
"""Add a label in front of the widget and put a frame around."""
if label == '':
# super(w, self).__init__(App.stack[-1], **kwargs)
# self = w(App.stack[-1], **kwargs)
w.__init__(self, App.stack[-1], **kwargs)
self.grid()
else:
fr = ttk.Frame(App.stack[-1])
ttk.Label(fr, text=label).grid()
self = w(fr, **kwargs)
self.grid(row=0, column=1)
fr.grid(sticky='e')

def add_command(self, cmd):
"""Add the function, or execute string via callback."""
self.cmd = cmd
Expand Down Expand Up @@ -639,10 +625,11 @@ class App(tk.Frame):

"""Define the application base class."""

def __init__(self, title='Tk'):
def __init__(self, title='Tk', debug=False):
root = tk.Tk()
root.option_add('*tearOff', False)

App.debug = debug
App.root = root
App.stack = [root]

Expand Down

0 comments on commit d24186e

Please sign in to comment.