Skip to content

Commit

Permalink
Add scrollbar to listbox
Browse files Browse the repository at this point in the history
  • Loading branch information
sharkykh committed Jul 20, 2018
1 parent 7fcfdff commit 284b01a
Showing 1 changed file with 43 additions and 1 deletion.
44 changes: 43 additions & 1 deletion tppm/ui/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,26 @@
from __future__ import absolute_import
from __future__ import unicode_literals

from Tkinter import Button, Entry, Label, Listbox, StringVar
from Tkinter import Button, Entry, Label, Listbox, Scrollbar, StringVar, TclError


# Based on: http://effbot.org/zone/tkinter-autoscrollbar.htm
class AutoScrollbar(Scrollbar):
"""A scrollbar that hides itself if it's not needed.
Only Works if you use the grid geometry manager."""
def set(self, lo, hi):
if float(lo) <= 0.0 and float(hi) >= 1.0:
# grid_remove is currently missing from Tkinter!
self.tk.call("grid", "remove", self)
else:
self.grid()
Scrollbar.set(self, lo, hi)

def pack(self, **kw):
raise TclError("cannot use pack with this widget")

def place(self, **kw):
raise TclError("cannot use place with this widget")


class MainUI(object):
Expand All @@ -24,6 +43,9 @@ def __init__(self, parent, app):
state="normal",
cursor="hand2",
)
self._listbox_scrollbar = AutoScrollbar(
self._listbox
)
self._label_header = Label(
parent,
font="{Segoe UI} 16 bold",
Expand Down Expand Up @@ -164,6 +186,13 @@ def __init__(self, parent, app):

# widget commands
self._listbox.bind('<<ListboxSelect>>', self._listbox_onselect)
self._listbox.config(
yscrollcommand=self._listbox_scrollbar.set
)
self._listbox_scrollbar.config(
orient='vertical',
command=self._listbox.yview
)
self._btnRefresh.configure(
command=self._btn_refresh_command
)
Expand All @@ -190,6 +219,19 @@ def __init__(self, parent, app):
rowspan=7,
sticky="nsew"
)
self._listbox.columnconfigure(
0,
weight=1
)
self._listbox.rowconfigure(
0,
weight=1
)
self._listbox_scrollbar.grid(
column=2,
rowspan=7,
sticky="ns"
)
self._label_header.grid(
in_=parent,
column=3,
Expand Down

0 comments on commit 284b01a

Please sign in to comment.