Created
October 9, 2017 07:12
-
-
Save jacobschaer/97516cae16343f0df210b9df911efd31 to your computer and use it in GitHub Desktop.
ListBox MultiWindow TkInter Example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Tkinter as tk | |
class Page(tk.Frame): | |
def __init__(self, *args, **kwargs): | |
tk.Frame.__init__(self, *args, **kwargs) | |
def show(self): | |
self.lift() | |
class Page1(Page): | |
def __init__(self, *args, **kwargs): | |
Page.__init__(self, *args, **kwargs) | |
label = tk.Label(self, text="This is page 1") | |
label.pack(side="top", fill="both", expand=True) | |
class Page2(Page): | |
def __init__(self, *args, **kwargs): | |
Page.__init__(self, *args, **kwargs) | |
label = tk.Label(self, text="This is page 2") | |
label.pack(side="top", fill="both", expand=True) | |
class Page3(Page): | |
def __init__(self, *args, **kwargs): | |
Page.__init__(self, *args, **kwargs) | |
label = tk.Label(self, text="This is page 3") | |
label.pack(side="top", fill="both", expand=True) | |
def pageselect(pages, event): | |
widget = event.widget | |
index = int(widget.curselection()[0]) | |
pages[index].show() | |
class MainView(tk.Frame): | |
def __init__(self, *args, **kwargs): | |
tk.Frame.__init__(self, *args, **kwargs) | |
p1 = Page1(self, borderwidth=2, highlightbackground="black") | |
p2 = Page2(self, borderwidth=2, highlightbackground="black") | |
p3 = Page3(self, borderwidth=2, highlightbackground="black") | |
pages = [p1, p2, p3] | |
selection_frame = tk.Frame(self) | |
message_frame = tk.Frame(self) | |
selection_frame.pack(side="left", fill="both", expand=False) | |
message_frame.pack(side="left", fill="both", expand=True) | |
p1.place(in_=message_frame, x=0, y=0, relwidth=1, relheight=1) | |
p2.place(in_=message_frame, x=0, y=0, relwidth=1, relheight=1) | |
p3.place(in_=message_frame, x=0, y=0, relwidth=1, relheight=1) | |
# b1 = tk.Button(buttonframe, text="Page 1", command=p1.lift) | |
# b2 = tk.Button(buttonframe, text="Page 2", command=p2.lift) | |
# b3 = tk.Button(buttonframe, text="Page 3", command=p3.lift) | |
selection_listbox = tk.Listbox(selection_frame) | |
selection_listbox.insert(1, "Page 1") | |
selection_listbox.insert(2, "Page 2") | |
selection_listbox.insert(3, "Page 3") | |
selection_listbox.pack(side="left", fill="both") | |
selection_listbox.bind('<<ListboxSelect>>', lambda event: pageselect(pages, event)) | |
# b1.pack(side="left") | |
# b2.pack(side="left") | |
# b3.pack(side="left") | |
p1.show() | |
if __name__ == "__main__": | |
root = tk.Tk() | |
main = MainView(root) | |
main.pack(side="left", fill="both", expand=True) | |
root.wm_geometry("800x400") | |
root.mainloop() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment