import os
from tkinter import *
from tkinter.ttk import *
from tkinter.font import *
from tkinter.simpledialog import *
from tkinter.messagebox import *
from tkinter.scrolledtext import ScrolledText
SIZE = 'size'
FONT = 'font'
WEIGHT = 'weight'
class packer():
def side_top(self): self.pack( side = TOP ); return self
def side_bottom(self): self.pack( side = BOTTOM ); return self
def side_left(self): self.pack( side = LEFT ); return self
def side_right(self): self.pack( side = RIGHT ); return self
def fill_both(self): self.pack( fill = BOTH ); return self
def fill(self): self.pack( fill = BOTH ); return self
def fill_x(self): self.pack( fill = X ); return self
def fill_y(self): self.pack( fill = Y ); return self
def expand(self): self.pack( expand = YES ); return self
def anchor_n(self): self.pack( anchor = N ); return self
def anchor_ne(self): self.pack( anchor = NE ); return self
def anchor_e(self): self.pack( anchor = E ); return self
def anchor_se(self): self.pack( anchor = SE ); return self
def anchor_s(self): self.pack( anchor = S ); return self
def anchor_sw(self): self.pack( anchor = SW ); return self
def anchor_w(self): self.pack( anchor = W ); return self
def anchor_nw(self): self.pack( anchor = NW ); return self
class grider():
def set_grid(self,r,c): self.grid(row=r,column=c); return self
def sticky_n(self): self.grid( sticky = N ); return self
def sticky_ne(self): self.grid( sticky = NE ); return self
def sticky_e(self): self.grid( sticky = E ); return self
def sticky_se(self): self.grid( sticky = SE ); return self
def sticky_s(self): self.grid( sticky = S ); return self
def sticky_sw(self): self.grid( sticky = SW ); return self
def sticky_w(self): self.grid( sticky = W ); return self
def sticky_nw(self): self.grid( sticky = NW ); return self
def sticky_ns(self): self.grid( sticky = NS ); return self
def sticky_ew(self): self.grid( sticky = EW ); return self
def sticky_nsew(self): self.grid( sticky = NSEW ); return self
def weightx(self,c,value): self.grid_columnconfigure(c,weight=value); return self
def weighty(self,r,value): self.grid_rowconfigure(r,weight=value); return self
def weight(self,r,c,value): self.weightx(c,value); self.weighty(r,value); return self
class widget(packer,grider):
def padx(self,value): self.config( padx = value ); return self;
def pady(self,value): self.config( pady = value ); return self;
def pad(self,value): self.config( padx = value, pady = value ); return self;
def ipad(self,value): self.config( ipadx = value, ipady = value ); return self;
def text(self,value): self.config(text = value); return self
def background(self,value): self.config(background = value); return self
def foreground(self,value): self.config(foreground = value); return self
def width(self,value): self.config(width = value); return self
def fontsize(self,value): self.font[SIZE] = value; self.config( font = self.font ); return self
def fontbold(self): self.font[WEIGHT] = BOLD; self.config( font = self.font ); return self
def border_flat(self,value): self.config( borderwidth=value, relief=FLAT ); return self
def border_raised(self,value): self.config( borderwidth=value, relief=RAISED ); return self
def border_sunken(self,value): self.config( borderwidth=value, relief=SUNKEN ); return self
def border_groove(self,value): self.config( borderwidth=value, relief=GROOVE ); return self
def border_RIDGE(self,value): self.config( borderwidth=value, relief=RIDGE ); return self
class window(Tk):
def __init__(self):
super().__init__()
class tab(Notebook,widget):
def __init__(self,parent):
super().__init__(parent)
def add_item(self,node,text):
self.add(node,text=text)
class split(PanedWindow,widget):
def __init__(self,parent,orient=HORIZONTAL):
super().__init__(parent,orient=orient)
def add_item(self,node):
self.add(node)
class hbox(Frame,widget):
def __init__(self,parent): super().__init__(parent)
def add_hbox(self): return hbox(self).side_left().fill_both()
def add_vbox(self): return vbox(self).side_left().fill_both()
def add_grid(self): return grid(self).side_left().fill_both()
def add_label(self,value): return label(self).text(value).side_left()
def add_button(self,value,handler=None): return button(self,value,handler).side_left()
def add_entry(self,value,handler=None): return entry(self,value,handler).side_left()
def add_list(self,handler=None): return list(self,handler).side_left()
def add_combo(self,values=None,handler=None): return combo(self,values=values,handler=handler).side_left()
def add_text(self): return text(self).side_left()
def add_table(self,columns=None,handler=None): return table(self,columns=columns,handler=handler).side_left()
class vbox(Frame,widget):
def __init__(self,parent): super().__init__(parent)
def add_hbox(self): return hbox(self).side_top().fill_both()
def add_vbox(self): return vbox(self).side_top().fill_both()
def add_grid(self): return grid(self).side_top().fill_both()
def add_label(self,value): return label(self,value).side_top()
def add_button(self,value,handler=None): return button(self,value,handler).side_top()
def add_entry(self,value,handler=None): return entry(self,value,handler).side_top()
def add_list(self,handler=None): return list(self,handler).side_top()
def add_combo(self,values=None,handler=None): return combo(self,values=values,handler=handler).side_top()
def add_text(self): return text(self).side_top()
def add_table(self,columns=None,handler=None): return table(self,columns=columns,handler=handler).side_top()
class grid(Frame,widget):
def __init__(self,parent): super().__init__(parent)
def add_hbox(self,r,c): return hbox(self).set_grid(r,c)
def add_vbox(self,r,c): return vbox(self).set_grid(r,c)
def add_grid(self,r,c): return grid(self).set_grid(r,c)
def add_label(self,value,r,c): return label(self).set_grid(r,c).text(value).sticky_nsew()
class label(Label,widget):
def __init__(self,parent,text=""):
super().__init__(parent,text=text)
self.font = Font()
class button(Button,widget):
def __init__(self,parent,text="",handler=None):
super().__init__(parent,text=text,command=handler)
self.font = Font()
class entry(Entry,widget):
def __init__(self,parent,text="",handler=None):
super().__init__(parent,text=text)
self.var = StringVar()
self.var.set(text)
self.font = Font()
self.config( textvariable = self.var )
if handler: self.bind( '<Return>', handler )
def get_text(self): return self.var.get()
def set_text(self,value): self.var.set(value)
class text(ScrolledText,widget):
def __init__(self,parent,values=None,handler=None):
super(text,self).__init__(parent)
class combo(Combobox,widget):
def __init__(self,parent,values=None,handler=None):
super(combo,self).__init__(parent)
self.var = StringVar()
self.config( textvariable = self.var )
self['values'] = values if values else []
if handler: self.bind('<<ComboboxSelected>>', handler)
if handler: self.bind('<Return>', handler)
def set_values(self,values): self['values'] = values; return self
def get_text(self): return self.var.get()
def set_text(self,value): self.var.set(value); return self
def readonly(self,value=True): self['state'] = 'readonly' if value == True else ''; return self
class list(Listbox,widget):
def __init__(self,parent,handler=None):
super(list,self).__init__(parent)
self.config(selectmode='extended')
self.config(height=0)
if handler: self.bind('<<ListboxSelect>>', handler)
scroll = Scrollbar(parent,orient=VERTICAL,command=self.yview)
scroll.pack( side=RIGHT, fill=Y )
self.config( yscrollcommand = scroll.set)
def add(self,value,index=END): self.insert(index,value); return self
class table(Treeview,widget):
row_color = ( 'white', 'gray')
def __init__(self,parent,columns=None,handler=None):
super(table,self).__init__(parent,columns=columns, show='headings')
if handler: self.bind('<<TreeviewSelect>>', handler)
scroll = Scrollbar(parent, orient=VERTICAL, command=self.yview)
#scroll.grid(row=0, column=1, sticky='ns')
scroll.pack( side=RIGHT, fill=Y )
self.configure(yscroll=scroll.set)
self.row_count = 0
self.tag_configure('white', background='#f0f0f0')
self.tag_configure('gray', background='#cccccc')
def column_name(self,name,title): self.heading(name,text=title); return self
def add_row(self,row,index=END): #, tag='gray'
self.insert('',END,values=row, tag=table.row_color[self.row_count % 2])
print(table.row_color[self.row_count % 2])
self.row_count += 1
def delete_row(self,item): self.delete(item) #self.tree.selection()[0]
def confirm_dialog(title,message):
showinfo( title=title, message=message )
if __name__ == '__main__':
def entry_handler(ev): print(ev.widget.get_text())
def get_button_handler(v): return lambda : print('Pressed ' + str(v))
def list_handler(ev): print(ev.widget.curselection())
def combo_handler(ev): print(ev.widget.get_text())
def table_handler(ev):
for selected_item in ev.widget.selection():
item = ev.widget.item(selected_item)
record = item['values']
print(','.join(record))
def get_layout_frame(parent):
v = vbox(parent).background("#ff00ff").pad(10).expand().fill_both()
v.add_label("packer_0").background("#ff0000").pad(10).anchor_w()
v.add_label("packer_1").background("#ffff00").pad(10).anchor_e()
h = v.add_hbox().background("#0000ff").pad(10).fill_both()
h.add_button("packer_2",get_button_handler(2)).background("#ff0000").pad(10)
h.add_button("packer_3",get_button_handler(3)).background("#ffff00").pad(10)
h.add_button("packer_4",get_button_handler(4)).background("#00ff00").pad(10).side_right()
g = v.add_grid().background("#ffff00").expand()
l = [[None]*3 for i in range(3)]
g.weightx(0,1).weighty(0,1)
for r in range(3):
for c in range(3):
l[r][c] = g.add_label("grid_{0}{1}".format(r,c),r,c)\
.background("#0{0}{1}".format(r*3,c*3))\
.foreground("#ffffff").pad(10)
l[0][0].fontsize(20).fontbold()
l[2][0].fontsize(30).fontbold()
l[2][2].fontsize(14).fontbold()
v.add_entry("Text",entry_handler).fill().fontsize(14).border_flat(5)
v.add_list(list_handler).add('One').add('Two').add('Three').fill()
return v
def get_widget_frame(parent):
v = vbox(parent).background("#ffff00").pad(10).expand().fill()
v.add_combo(handler=combo_handler).set_values(('one','two','three')).anchor_w().set_text('four')
v.add_text().width(40).fill().expand()
tb = v.add_table(columns=('one','two'),handler=table_handler).fill().expand()
tb.column_name('one','One')
tb.column_name('two','Two')
tb.add_row(('first1','second1'))
tb.add_row(('first2','second2'))
tb.add_row(('first3','second3'))
tb.add_row(('first4','second4'))
return v
def get_widget_frame2(parent):
f=Frame(parent)
tv=Treeview(f,show='tree')
ybar=Scrollbar(f,orient=VERTICAL, command=tv.yview)
tv.configure(yscroll=ybar.set)
directory='D:/Ebook'
tv.heading('#0',text='Dir:'+directory,anchor='w')
path=os.path.abspath(directory)
node=tv.insert('','end',text=path,open=True)
def traverse_dir(parent,path):
for d in os.listdir(path):
full_path=os.path.join(path,d)
isdir = os.path.isdir(full_path)
id=tv.insert(parent,'end',text=d,open=False)
if isdir:
traverse_dir(id,full_path)
traverse_dir(node,path)
ybar.pack(side=RIGHT,fill=Y)
tv.pack(expand=1, fill=BOTH)
f.pack(expand=1, fill=BOTH)
return f
w = window()
t = tab(w).expand().fill_both()
s = split(t).expand().fill_both()
v = get_layout_frame(t)
v2 = get_widget_frame(t)
v3 = get_widget_frame2(t)
s.add_item(v2)
s.add_item(v3) #label(s,'b').expand().fill_both().background('#ff00ff').border_groove(1))
s.pack()
t.add_item(v,'Widgets')
t.add_item(s,'Split')
t.pack()
w.mainloop()
#https://www.pythontutorial.net/tkinter/