two-windows.html


001| ####################################################################################################
002| from sys import argv, stdout, stderr, path as syspath
003| from os import path
004| from datetime import datetime, date, timedelta
005| import tkinter as tk
006| import tkinter.ttk as ttk
007| ####################################################################################################
008| BasePath = r'C:\CJS\prj\Python\ws\tk'
009| 
010| bag = {}
011| 
012| ##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~##
013| class Application (tk.Frame):
014| 
015|     def __init__(self, **kwargs):
016|         self.root = tk.Tk()
017|         self.root.geometry('400x200+2000+400')
018|         self.widgets()
019|         self.menubar()
020|         self.pack()
021|         bag['app'] = self
022| 
023|     def quit (self):
024|         print("Quit!")
025|         self.root.destroy()
026| 
027|     def cmd_one (self):
028|         print("command one")
029| 
030|     def cmd_two (self):
031|         print("command two")
032| 
033|     def cmd_three (self):
034|         print("command three")
035| 
036|     def menu_selection (self, which):
037|         print(f"Menu Selection: {which}")
038| 
039|     def widgets (self):
040| 
041|         ttk.Style().configure('C.TButton', foreground='#0000ff', fontsize=12, fontweight='bold')
042|         ttk.Style().configure('Q.TButton', foreground='#ff0000', fontsize=12, fontweight='bold')
043| 
044|         # Window #1...
045|         self.top = tk.Toplevel(self.root)
046|         self.top.geometry('300x200+2000+100')
047| 
048|         self.btn1  = ttk.Button(self.top, style='C.TButton', text="Command 1", command=self.cmd_one  )
049|         self.btn2  = ttk.Button(self.top, style='C.TButton', text="Command 2", command=self.cmd_two  )
050|         self.btn3  = ttk.Button(self.top, style='C.TButton', text="Command 3", command=self.cmd_three)
051|         self.bQuit = ttk.Button(self.top, style='Q.TButton', text='Quit'     , command=self.quit     )
052| 
053|         self.btn1.pack(side='top', anchor='e', fill='x')
054|         self.btn2.pack(side='top', anchor='e', fill='x')
055|         self.btn3.pack(side='top', anchor='e', fill='x')
056|         self.bQuit.pack(side='bottom', anchor='e', fill='both', expand=True)
057| 
058|         # Window #2...
059|         self.frm = tk.Frame.__init__(self, self.root)
060|         print(f'{self.frm=}')
061| 
062|         self.lst1   = tk.Listbox(self.frm)
063|         self.b2b1   = ttk.Button(self.frm, style='C.TButton', text='Command 1', command=self.cmd_one)
064|         self.b2Quit = ttk.Button(self.frm, style='Q.TButton', text='Quit', command=self.quit)
065| 
066|         self.lst1.pack(side="left", anchor='n', fill='y')
067|         self.b2b1.pack(side='top', anchor='n', fill='x')
068|         self.b2Quit.pack(side='top', anchor='e')
069| 
070|         self.lst1.insert(0, "foobar barfoo", 'eggdom imegon',"nubish rondif", 'spams alot')
071|         self.b2b1["text"] = "Download today's Stats and Boxscores!"
072| 
073|     def menubar (self):
074|         def menu_command_proxy (which):
075|             return lambda: self.menu_selection(which)
076| 
077|         # Window #1 MenuBar...
078|         self.mbar = tk.Menu(self.root)
079| 
080|         # File...
081|         self.mnuFile = tk.Menu(self.mbar, name='fileMenu', tearoff=False)
082|         self.mnuFile.add_command(label='New', underline=0, command=menu_command_proxy('File.New'))
083|         self.mnuFile.add_command(label='Open...', underline=0, command=menu_command_proxy('File.Open'))
084|         self.mnuFile.add_command(label='Save', underline=0, command=menu_command_proxy('File.Save'))
085|         self.mnuFile.add_separator()
086|         self.mnuFile.add_command(label='Exit', underline=1, command=self.quit)
087| 
088|         # Edit...
089|         self.mnuEdit = tk.Menu(self.mbar, name='editMenu', tearoff=False)
090|         self.mnuEdit.add_command(label='Copy', underline='0', command=menu_command_proxy('Edit.Copy'))
091|         self.mnuEdit.add_command(label='Paste', underline='0', command=menu_command_proxy('Edit.Paste'))
092| 
093|         # View...
094|         self.mnuView = tk.Menu(self.mbar, name='viewMenu', tearoff=False)
095|         self.mnuView.add_command(label='Info', underline='0', command=menu_command_proxy('View.Info'))
096| 
097|         # Add to MenuBar...
098|         self.mbar.add_cascade(label='File', menu=self.mnuFile, underline=1)
099|         self.mbar.add_cascade(label='Edit', menu=self.mnuEdit, underline=1)
100|         self.mbar.add_cascade(label='View', menu=self.mnuView, underline=1)
101| 
102|         self.root['menu'] = self.mbar
103| 
104|         # Window #2 MenuBar...
105|         self.mbar2 = tk.Menu(self.frm, name='menuExit')
106|         self.mbar2.add_command(label='Exit', underline='0', command=menu_command_proxy('Exit'))
107|         #self.frm['menu'] = self.mbar2
108| 
109| 
110| 
111| ##================================================================================================##
112| def do_test (*args):
113|     print(f'test/parameters: {len(args)}')
114|     return args
115| ##================================================================================================##
116| def do_main (*args):
117|     print(f'main/parameters: {len(args)}')
118| 
119|     app = Application()
120|     app.mainloop()
121| 
122|     return args
123| ####################################################################################################
124| def dispatch (cmd, *args):
125|     print(f'command: {cmd}')
126|     print(f'arguments: {len(args)}')
127|     if cmd == 'main': return do_main(*args)
128|     if cmd == 'test': return do_test(*args)
129|     return [None, cmd, args]
130| ####################################################################################################
131| if __name__ == '__main__':
132|     print(f'autorun: {argv[0]}')
133|     cmd = argv[1] if 1 < len(argv) else 'main'
134|     etc = argv[2:]
135|     obj = dispatch(cmd, *etc)
136|     print('')
137|     print(f'exit/type: {type(obj)} (length: {len(obj)})')
138| ####################################################################################################
139| '''eof'''
140|