1
0
Fork 0
mirror of https://github.com/shlomif/PySolFC.git synced 2025-04-05 00:02:29 -04:00
This commit is contained in:
Shlomi Fish 2017-04-18 20:49:35 +03:00
parent e3e3f75068
commit f60648a642
2 changed files with 66 additions and 54 deletions

View file

@ -1,6 +1,6 @@
#!/usr/bin/env python
# -*- mode: python; coding: utf-8; -*-
# ---------------------------------------------------------------------------##
# ---------------------------------------------------------------------------
#
# Copyright (C) 1998-2003 Markus Franz Xaver Johannes Oberhumer
# Copyright (C) 2003 Mt. Hood Playing Card Co.
@ -19,35 +19,44 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# ---------------------------------------------------------------------------##
# ---------------------------------------------------------------------------
"""
a couple of classes for implementing partial tabbed-page like behaviour
"""
from Tkinter import *
from Tkinter import EW, FALSE, Frame, Radiobutton, RAISED, RIDGE, StringVar
from Tkinter import BOTH, Button, Entry, Label, LEFT, NSEW, Tk, TRUE
MYRIDGE, MYRAISED = RAISED, RIDGE
#MYRIDGE, MYRAISED = RIDGE, RAISED
# MYRIDGE, MYRAISED = RIDGE, RAISED
class InvalidTabPage(Exception):
pass
class AlreadyExists(Exception):
pass
class InvalidTabPage(Exception): pass
class AlreadyExists(Exception): pass
class PageTab(Frame):
"""
a 'page tab' like framed button
"""
def __init__(self,parent):
def __init__(self, parent):
Frame.__init__(self, parent, borderwidth=2, relief=MYRIDGE)
self.button=Radiobutton(self, padx=5, pady=5, takefocus=0,
indicatoron=FALSE, highlightthickness=0,
borderwidth=0, selectcolor=self.cget('bg'))
self.button = Radiobutton(
self, padx=5, pady=5, takefocus=0,
indicatoron=FALSE, highlightthickness=0,
borderwidth=0, selectcolor=self.cget('bg'))
self.button.pack()
class TabPageSet(Frame):
"""
a set of 'pages' with TabButtons for controlling their display
"""
def __init__(self,parent,pageNames=[],**kw):
def __init__(self, parent, pageNames=[], **kw):
"""
pageNames - a list of strings, each string will be the dictionary key
to a page's data, and the name displayed on the page's tab. Should be
@ -55,39 +64,39 @@ class TabPageSet(Frame):
and first active page.
"""
Frame.__init__(self, parent, kw)
self.grid_location(0,0)
self.columnconfigure(0,weight=1)
self.rowconfigure(1,weight=1)
self.tabBar=Frame(self)
self.tabBar.grid(row=0,column=0,sticky=EW)
self.activePage=StringVar(self)
self.defaultPage=''
self.pages={}
self.grid_location(0, 0)
self.columnconfigure(0, weight=1)
self.rowconfigure(1, weight=1)
self.tabBar = Frame(self)
self.tabBar.grid(row=0, column=0, sticky=EW)
self.activePage = StringVar(self)
self.defaultPage = ''
self.pages = {}
for name in pageNames:
self.AddPage(name)
def ChangePage(self,pageName=None):
def ChangePage(self, pageName=None):
if pageName:
if pageName in self.pages.keys():
self.activePage.set(pageName)
else:
raise InvalidTabPage, 'Invalid TabPage Name'
## pop up the active 'tab' only
raise InvalidTabPage('Invalid TabPage Name')
# pop up the active 'tab' only
for page in self.pages.keys():
self.pages[page]['tab'].config(relief=MYRIDGE)
self.pages[self.GetActivePage()]['tab'].config(relief=MYRAISED)
## switch page
# switch page
self.pages[self.GetActivePage()]['page'].lift()
def GetActivePage(self):
return self.activePage.get()
def AddPage(self,pageName):
def AddPage(self, pageName):
if pageName in self.pages.keys():
raise AlreadyExists, 'TabPage Name Already Exists'
self.pages[pageName]={
raise AlreadyExists('TabPage Name Already Exists')
self.pages[pageName] = {
'tab': PageTab(self.tabBar),
'page': Frame(self,borderwidth=2,relief=RAISED)
'page': Frame(self, borderwidth=2, relief=RAISED)
}
self.pages[pageName]['tab'].button.config(
text=pageName,
@ -96,47 +105,50 @@ class TabPageSet(Frame):
value=pageName
)
self.pages[pageName]['tab'].pack(side=LEFT)
self.pages[pageName]['page'].grid(row=1,column=0,sticky=NSEW)
if len(self.pages)==1: # adding first page
self.defaultPage=pageName
self.pages[pageName]['page'].grid(row=1, column=0, sticky=NSEW)
if len(self.pages) == 1: # adding first page
self.defaultPage = pageName
self.activePage.set(self.defaultPage)
self.ChangePage()
def RemovePage(self,pageName):
if not pageName in self.pages.keys():
raise InvalidTabPage, 'Invalid TabPage Name'
def RemovePage(self, pageName):
if pageName not in self.pages.keys():
raise InvalidTabPage('Invalid TabPage Name')
self.pages[pageName]['tab'].pack_forget()
self.pages[pageName]['page'].grid_forget()
self.pages[pageName]['tab'].destroy()
self.pages[pageName]['page'].destroy()
del(self.pages[pageName])
# handle removing last remaining, or default, or active page
if not self.pages: # removed last remaining page
self.defaultPage=''
if not self.pages: # removed last remaining page
self.defaultPage = ''
return
if pageName==self.defaultPage: # set a new default page
self.defaultPage=\
if pageName == self.defaultPage: # set a new default page
self.defaultPage = \
self.tabBar.winfo_children()[0].button.cget('text')
if pageName==self.GetActivePage(): # set a new active page
if pageName == self.GetActivePage(): # set a new active page
self.activePage.set(self.defaultPage)
self.ChangePage()
if __name__ == '__main__':
#test dialog
root=Tk()
tabPage=TabPageSet(root,pageNames=['Foobar','Baz'])
tabPage.pack(expand=TRUE,fill=BOTH)
Label(tabPage.pages['Foobar']['page'],text='Foo',pady=20).pack()
Label(tabPage.pages['Foobar']['page'],text='Bar',pady=20).pack()
Label(tabPage.pages['Baz']['page'],text='Baz').pack()
entryPgName=Entry(root)
buttonAdd=Button(root,text='Add Page',
command=lambda:tabPage.AddPage(entryPgName.get()))
buttonRemove=Button(root,text='Remove Page',
command=lambda:tabPage.RemovePage(entryPgName.get()))
labelPgName=Label(root,text='name of page to add/remove:')
buttonAdd.pack(padx=5,pady=5)
buttonRemove.pack(padx=5,pady=5)
# test dialog
root = Tk()
tabPage = TabPageSet(root, pageNames=['Foobar', 'Baz'])
tabPage.pack(expand=TRUE, fill=BOTH)
Label(tabPage.pages['Foobar']['page'], text='Foo', pady=20).pack()
Label(tabPage.pages['Foobar']['page'], text='Bar', pady=20).pack()
Label(tabPage.pages['Baz']['page'], text='Baz').pack()
entryPgName = Entry(root)
buttonAdd = Button(
root, text='Add Page',
command=lambda: tabPage.AddPage(entryPgName.get()))
buttonRemove = Button(
root, text='Remove Page',
command=lambda: tabPage.RemovePage(entryPgName.get()))
labelPgName = Label(root, text='name of page to add/remove:')
buttonAdd.pack(padx=5, pady=5)
buttonRemove.pack(padx=5, pady=5)
labelPgName.pack(padx=5)
entryPgName.pack(padx=5)
tabPage.ChangePage()

View file

@ -27,7 +27,7 @@ my %skip =
# my $cmd = shell_quote( 'flake8', '.' );
my $cmd = shell_quote( 'flake8',
grep { not exists $skip{$_} } glob('./*.py ./scripts/*.py ./tests/board_gen/*.py ./pysollib/*.py ./pysollib/[cmgpuw]*/{*/*.py,*.py} ./pysollib/tile/*.py ./pysollib/tk/{[a-sw]}*.py ./pysollib/ui/tktile/*.py') );
grep { not exists $skip{$_} } glob('./*.py ./scripts/*.py ./tests/board_gen/*.py ./pysollib/*.py ./pysollib/[cmgpuw]*/{*/*.py,*.py} ./pysollib/tile/*.py ./pysollib/tk/{[a-sw],ta}*.py ./pysollib/ui/tktile/*.py') );
# TEST
eq_or_diff( scalar(`$cmd`), '', "flake8 is happy with the code." );