Reputation: 23
I am new to programming, and I am having trouble finding a tutorial that teaches how to create a GUI that uses multiple windows. For example, If a user clicks a "lookup" button, a window pops up with the search results. How do I accomplish this? Is this possible within Tkinter? Any suggestions/ references to sources would be greatly appreciated. Thanks.
Upvotes: 2
Views: 6526
Reputation: 386342
To create your first window, you create an instance of the Tk
class. All other windows are instances of Toplevel
.
import Tkinter as tk
class Example(tk.Frame):
def __init__(self, root):
tk.Frame.__init__(self, root)
b1 = tk.Button(self, text="Add another window", command = self.newWindow)
b1.pack(side="top", padx=40, pady=40)
self.count = 0
def newWindow(self):
self.count += 1
window = tk.Toplevel(self)
label = tk.Label(window, text="This is window #%s" % self.count)
label.pack(side="top", fill="both", expand=True, padx=40, pady=40);
if __name__ == "__main__":
root = tk.Tk()
Example(root).pack(side="top", fill="both", expand=True)
root.mainloop()
Upvotes: 2
Reputation: 19
import tkinter as tk
from tkinter import *
class Example(tk.Frame):
def __init__(self, root):
tk.Frame.__init__(self, root)
# b1 = tk.Button(self, text="Add another window", command = self.newWindow)
self.count = 0
self.canvas1 = Canvas(self, width=500, height=500)
b1 = tk.Button(self.canvas1, text="Add another window", command = self.create_new_window)
# b1.pack(side="top", padx=40, pady=40)
self.canvas1.pack(side="top")
b1.pack(side="top", padx=250, pady=240)
def create_new_window(self):
self.window1 = tk.Toplevel(self)
self.window1.geometry("+160+300")
self.canvas1 = Canvas(self.window1, width=50, height=500)
# label = tk.Label(self.window1, text="This is window #%s" % self.count)
self.canvas1.pack(side="top", fill="both")
def create_new_window2(self):
self.window2 = tk.Toplevel(self)
self.canvas2 = Canvas(self.window2, width=500, height=500)
# label = tk.Label(self.window2, text="This is window #%s" % self.count)
self.canvas2.pack(side="top", fill="both", expand=True, padx=40, pady=40)
if __name__ == "__main__":
root = tk.Tk()
root.geometry("+300+300")
Example(root).pack(side="top", fill="both", expand=True)
root.mainloop()
Upvotes: 1