Skip to content

Instantly share code, notes, and snippets.

@zaxboy24
Created January 31, 2019 12:34
Show Gist options
  • Save zaxboy24/963035ca28ae4a631bb4fcd29f8794e7 to your computer and use it in GitHub Desktop.
Save zaxboy24/963035ca28ae4a631bb4fcd29f8794e7 to your computer and use it in GitHub Desktop.
How to call the child window using parent method in tkinter? Can anyone help me please.
import tkinter as tk
import tkMessageBox
import ttk
class Track(tk.Frame):
def __init__(self, master):
tk.Frame.__init__(self, master)
x = (self.master.winfo_screenwidth() - self.master.winfo_reqwidth()) / 2
y = (self.master.winfo_screenheight() - self.master.winfo_reqheight()) / 2
self.master.geometry("+{}+{}".format(x, y))
self.pack()
self.master.resizable(False, False)
self.master.title("Sample")
self.main_container()
def main_container(self):
tk.Message(self, text = "Person", font = 'arial 20 bold', justify = 'center', aspect = 800 ).pack(pady = (15, 0), side = 'top')
dialog_frame = tk.Frame(self, relief = 'raise', bg = 'lightgrey', height = 100, bd = 10)
dialog_frame.pack(padx = 10, pady = 5, anchor = 'w')
tk.Label(dialog_frame, text = "Name: ", bg = 'lightgrey', font = 'arial 13').grid(row = 0, column = 0, sticky = 'w')
self.name = tk.Entry(dialog_frame, fg = 'black',font = 'arial 13', width = 25, bd = 3, relief = "sunken")
self.name.grid(row = 0, column = 1, sticky = 'w')
tk.Label(dialog_frame, bg = 'lightgrey', text = "Address: " , font = 'arial 13').grid(row = 1, column = 0, sticky = 'w')
self.address = tk.Entry(dialog_frame, fg = 'black',font = 'arial 13', width = 25, bd = 3, relief = "sunken")
self.address.grid(row = 1, column = 1, sticky = 'w')
tk.Label(dialog_frame, bg = 'lightgrey', text = "Gender: ", font = 'arial 13').grid(row = 2, column = 0, sticky = 'w')
self.check1 = tk.Checkbutton(dialog_frame, text = 'Male', bg = 'lightgrey', onvalue = 1,font = 'arial 13', offvalue = 0, height = 1, width = 5, cursor = 'sizing', activebackground = 'lightgreen')
self.check1.grid(row = 2, column = 1, sticky = 'w')
self.check1 = tk.Checkbutton(dialog_frame, text = 'Female', bg = 'lightgrey', font = 'arial 13', onvalue = 1, offvalue = 0, height = 1, width = 5, cursor = 'heart', activebackground = 'pink')
self.check1.grid(row = 2, column = 1, sticky = 'e')
tk.Label(dialog_frame, text = "Contact #: " , bg = 'lightgrey', font = 'arial 13').grid(row = 3, column = 0, sticky = 'w')
self.contact = tk.Entry(dialog_frame, fg = 'black', width = 25,bd = 3, relief = "sunken", font = 'arial 13')
self.contact.grid(row = 3, column = 1, sticky = 'w')
tk.Label(dialog_frame, text = "Track:", bg = 'lightgrey', font = 'arial 13').grid(row = 4, column = 0, sticky = 'w')
self.chooser = ttk.Combobox(dialog_frame,width = 25)
self.chooser['values'] = ("Faculty", "Staff", "Student")
self.chooser.grid(row = 4, column = 1, sticky = 'w')
#------------------------------- Botton Frame --------------------------
button_frame = tk.Frame(self)
button_frame.pack(padx = 10, pady = 1, anchor = 'n')
self.buttonSave = tk.Button(button_frame, text = 'save',font = 'arial 13', activebackground = 'green', cursor = 'cross',bd = 3, command = self.error, relief = "raised")
self.buttonSave.grid(row = 0, column = 1, sticky = 'w')
self.buttonCancel = tk.Button(button_frame, text = 'cancel',font = 'arial 13', activebackground = 'red', cursor = 'circle',bd = 3, command = self.clickcancel, relief = "raised")
self.buttonCancel.grid(row = 0, column = 2, sticky = 'e')
#------------------------------------ Methods --------------------------
#--------------------------------- Destroy Function -----------------------
def clickcancel(self):
self.master.destroy()
#------------------------------------ Error Function --------------------------
def error(self):
if self.chooser.get() == '' or self.name.get() == '' or self.address.get() == '' or self.contact.get() == '':
tkMessageBox.showwarning("Incomplete", "Please fill all the Availabe Fields")
else:
return self.cont()
def save(self):
if self.entry1.get() == '' or self.entry2.get() == '':
tkMessageBox.showwarning("Incomplete", "Please fill all the Availabe Fields")
else:
tkMessageBox.showinfo("Succes", "Register Complete!")
self.master.destroy()
#------------------------------------ Filter Function --------------------------
def cont(self):
self.grabstud = Student()
self.grabfac = Faculty()
self.grabstaff = Staff()
if self.chooser.get() == "Faculty":
return self.grabfac()
elif self.chooser.get() == "Student":
return self.grabstud()
elif self.chooser.get() == "Staff":
return self.grabstaff()
class Student(Track):
def __init__(self, master):
super().__init__(master)
self.student_container()
def student_container(self):
self.master.title("Student Program")
tk.Message(self, text = "Student", font = 'arial 20 bold', justify = 'center', aspect = 800 ).pack(pady = (15, 0), side = 'top')
dialog_frame = tk.Frame(self, relief = 'raise', bg = 'lightgrey', height = 100, bd = 10)
dialog_frame.pack(padx = 10, pady = 5, anchor = 'w')
tk.Label(dialog_frame, text = "Course: ", bg = 'lightgrey', font = 'arial 13').grid(row = 0, column = 0, sticky = 'w')
self.entry1 = tk.Entry(dialog_frame, fg = 'black',font = 'arial 13', width = 25, bd = 3, relief = "sunken")
self.entry1.grid(row = 0, column = 1, sticky = 'w')
tk.Label(dialog_frame, bg = 'lightgrey', text = "Major: " , font = 'arial 13').grid(row = 1, column = 0, sticky = 'w')
self.entry2 = tk.Entry(dialog_frame, fg = 'black',font = 'arial 13', width = 25, bd = 3, relief = "sunken")
self.entry2.grid(row = 1, column = 1, sticky = 'w')
button_frame = tk.Frame(self)
button_frame.pack(padx = 10, pady = 1, anchor = 'n')
self.buttonSave = tk.Button(button_frame, text = 'Save',font = 'arial 13', activebackground = 'green', cursor = 'cross', command = self.save, relief = "raised")
self.buttonSave.grid(row = 0, column = 1, sticky = 'w')
self.buttonCancel = tk.Button(button_frame, text = 'Cancel',font = 'arial 13', activebackground = 'red', cursor = 'circle', command = self.cancel, relief = "raised")
self.buttonCancel.grid(row = 0, column = 2, sticky = 'e')
def cancel(self):
self.student_container.destroy()
class Faculty(Track):
def __init__(self, master):
super().__init__(master)
self.faculty_container()
def faculty_container(self):
self.master.title("Faculty Program")
tk.Message(self, text = "Faculty", font = 'arial 20 bold', justify = 'center', aspect = 800 ).pack(pady = (15, 0), side = 'top')
dialog_frame = tk.Frame(self, relief = 'raise', bg = 'lightgrey', height = 100, bd = 10)
dialog_frame.pack(padx = 10, pady = 5, anchor = 'w')
tk.Label(dialog_frame, text = "Academic Rank: ", bg = 'lightgrey', font = 'arial 13').grid(row = 0, column = 0, sticky = 'w')
self.entry1 = tk.Entry(dialog_frame, fg = 'black',font = 'arial 13', width = 25, bd = 3, relief = "sunken")
self.entry1.grid(row = 0, column = 1, sticky = 'w')
tk.Label(dialog_frame, bg = 'lightgrey', text = "Academic research: " , font = 'arial 13').grid(row = 1, column = 0, sticky = 'w')
self.entry2 = tk.Entry(dialog_frame, fg = 'black',font = 'arial 13', width = 25, bd = 3, relief = "sunken")
self.entry2.grid(row = 1, column = 1, sticky = 'w')
button_frame = tk.Frame(self)
button_frame.pack(padx = 10, pady = 1, anchor = 'n')
self.buttonSave = tk.Button(button_frame, text = 'Save',font = 'arial 13', activebackground = 'green', cursor = 'cross', command = self.save, relief = "raised")
self.buttonSave.grid(row = 0, column = 1, sticky = 'w')
self.buttonCancel = tk.Button(button_frame, text = 'Cancel',font = 'arial 13', activebackground = 'red', cursor = 'circle', command = self.cancel, relief = "raised")
self.buttonCancel.grid(row = 0, column = 2, sticky = 'e')
def cancel(self):
self.faculty_container.destroy()
class Staff(Track):
def __init__(self, master):
super().__init__(master)
self.staff_container()
def staff_container(self):
self.master.title("Staff Program")
tk.Message(self, text = "Staff", font = 'arial 20 bold', justify = 'center', aspect = 800 ).pack(pady = (15, 0), side = 'top')
dialog_frame = tk.Frame(self, relief = 'raise', bg = 'lightgrey', height = 100, bd = 10)
dialog_frame.pack(padx = 10, pady = 5, anchor = 'w')
tk.Label(dialog_frame, text = "Position: ", bg = 'lightgrey', font = 'arial 13').grid(row = 0, column = 0, sticky = 'w')
self.entry1 = tk.Entry(dialog_frame, fg = 'black',font = 'arial 13', width = 25, bd = 3, relief = "sunken")
self.entry1.grid(row = 0, column = 1, sticky = 'w')
tk.Label(dialog_frame, bg = 'lightgrey', text = "office: " , font = 'arial 13').grid(row = 1, column = 0, sticky = 'w')
self.entry2 = tk.Entry(dialog_frame, fg = 'black',font = 'arial 13', width = 25, bd = 3, relief = "sunken")
self.entry2.grid(row = 1, column = 1, sticky = 'w')
button_frame = tk.Frame(self)
button_frame.pack(padx = 10, pady = 1, anchor = 'n')
self.buttonSave = tk.Button(button_frame, text = 'Save',font = 'arial 13', activebackground = 'green', cursor = 'cross', command = self.save, relief = "raised")
self.buttonSave.grid(row = 0, column = 1, sticky = 'w')
self.buttonCancel = tk.Button(button_frame, text = 'Cancel',font = 'arial 13', activebackground = 'red', cursor = 'circle', command = self.cancel, relief = "raised")
self.buttonCancel.grid(row = 0, column = 2, sticky = 'e')
def cancel(self):
self.staff_container.destroy()
print(help(Student))
if __name__ == '__main__':
root = tk.Tk()
app = Track(root)
app.mainloop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment