I am trying to make a fairly simple GUI with multiple windows. I have my windows built as classes just with a label in each one for now. I can't seem to figure out why when I run my program it packs all of the labels on the "StartPage" and none of the other windows have anything in them. It could be that I have my classes configured incorrectly?
import tkinter as tk
class application(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
container = tk.Frame(self)
container.pack(side = 'top', fill = 'both', expand = True)
container.grid_rowconfigure(0, weight = 1)
container.grid_columnconfigure(0, weight = 1)
self.frames = {}
for F in (StartPage, WeeklyBudget, LongtermSavings, Investments):
frame = F(container, self)
self.frames[F] = frame
frame.grid(row=0, column=0, sticky="nsew")
self.ShowFrame(StartPage)
def ShowFrame(self, cont):
frame = self.frames[cont]
frame.tkraise()
class StartPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
start_label = tk.Label(self, text = 'Welcome to Finance Track!')
start_label.pack()
week_btn = tk.Button(self, text = 'Weekly Budgeting', command =lambda: controller.ShowFrame(WeeklyBudget))
savings_btn = tk.Button(self, text = 'Longterm Savings', command = lambda: controller.ShowFrame(LongtermSavings))
invest_btn = tk.Button(self, text = 'Investments', command = lambda: controller.ShowFrame(Investments))
week_btn.pack(pady = 10, padx = 10)
savings_btn.pack(pady = 10, padx = 10)
invest_btn.pack(pady = 10, padx = 10)
class WeeklyBudget(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
label = tk.Label(text = 'Welcome to your Weekly Budget')
label.pack()
add_btn = tk.Button(text = 'add new week')
add_btn.pack()
class LongtermSavings(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
label = tk.Label(text = 'Welcome to your Longterm Savings')
label.pack()
class Investments(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
label = tk.Label(text = 'Welcome to your Investments')
label.pack()
app = application()
app.mainloop()
The current outcome as I described earlier is just one window with all the labels and all the buttons in it.
As jasonharper mention you aren't defining the parents (aka master) of many widgets.
class Investments(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
label = tk.Label(text = 'Welcome to your Investments')
label.pack()
Using this Investments class for example, your label will be given the window as it's parents by default, to set it's parent to the newly created frame, just do the following:
class Investments(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
label = tk.Label(self, text = 'Welcome to your Investments')
label.pack()
Related
I am trying to build simple GUI using tkinter in Python. On the page "Home" I need my buttons to be centered, but I can't figure out how to do that. I don't want to use place, cause I want to learn how to use grid system properly.
This is the code I have so far:
import tkinter as tk
class Main(tk.Tk):
def __init__(self, *args, **kwargs):
tasks = [Home, Zad1, Zad2, Zad3, Zad4, Zad5, Zad6, Zad7]
tk.Tk.__init__(self, *args, **kwargs)
container = tk.Frame(self)
container.pack(side='top', fill='both', expand=True)
container.grid(column=0, row=0)
# container.grid_rowconfigure(0, weight=1)
# container.grid_columnconfigure(0, weight=1)
self.frames = {}
for shown in tasks:
shown_name = shown.__name__
frame = shown(parent=container, controller=self)
self.frames[shown_name] = frame
frame.grid(row='0', column='0',sticky='snew')
self.show_frame('Home')
def show_frame(self, shown_name):
frame = self.frames[shown_name]
frame.tkraise()
class Home(tk.Frame):
def __init__(self, parent ,controller, **kw):
super().__init__(**kw)
self.controller = controller
self.buttons()
def buttons(self):
tasks = [Zad1, Zad2, Zad3, Zad4, Zad5, Zad6, Zad7]
i = 1
r = 0
for page in tasks:
page_name = page.__name__
button = tk.Button(self,
text=i, width=10,
command=lambda page_name=page_name:
self.controller.show_frame(page_name)).grid(row=r,column=0,pady=15)
i+=1
r+=1
class Zad1(tk.Frame):
def __init__(self, parent, controller, **kw):
super().__init__(**kw)
self.controller = controller
text = tk.IntVar()
label = tk.Label(self, text="brutto:")
brutto_input = tk.Entry(self, textvariable=text)
show = tk.Button(self, text="Show",width=10, command=lambda: self.netto(text))
home = tk.Button(self, text="Home",width=10, command=lambda: controller.show_frame('Home'))
label.grid(row=0,column=0)
brutto_input.grid(row=0,column=1)
show.grid(row=0,column=2)
home.grid(row=1,column=2)
def netto(self, text):
self.text = text
text_get = text.get()
netto_output = tk.Label(self, height=2, width=15, text=f'Sum: {text_get+1}')
netto_output.grid(row=0,column=3)
netto_output.config(text=f'Sum: {text_get+1}')
class Zad2(tk.Frame):
def __init__(self, parent, controller, **kw):
super().__init__(**kw)
self.controller = controller
label = tk.Label(self, text="2:")
class Zad3(tk.Frame):
def __init__(self, parent, controller, **kw):
super().__init__(**kw)
self.controller = controller
label = tk.Label(self, text="3:")
class Zad4(tk.Frame):
def __init__(self, parent, controller, **kw):
super().__init__(**kw)
self.controller = controller
label = tk.Label(self, text="4:")
class Zad5(tk.Frame):
def __init__(self, parent, controller, **kw):
super().__init__(**kw)
self.controller = controller
label = tk.Label(self, text="5:")
class Zad6(tk.Frame):
def __init__(self, parent, controller, **kw):
super().__init__(**kw)
self.controller = controller
label = tk.Label(self, text="6:")
class Zad7(tk.Frame):
def __init__(self, parent, controller, **kw):
super().__init__(**kw)
self.controller = controller
label = tk.Label(self, text="8:")
if __name__ == "__main__":
app = Main()
app.title('Last Test')
app.geometry('500x500')
app.mainloop()
And here is how it looks
I also have all classes from the list tasks[] made, but I didn't include them, couse they are empty.
The mods I made are marked with # (*** 1 ***), # (*** 2 ***), etc.
There are 5 in total.
I don't have enough reputation to post images.
Anyway, here is a link to how it looked:
https://i.postimg.cc/DzHy1S5t/cant-center-butttons-using-grid-tkinter.png
import tkinter as tk
class Main(tk.Tk):
def __init__(self, *args, **kwargs):
tasks = [Home, Zad1, Zad2, Zad3, Zad4, Zad5, Zad6, Zad7]
tk.Tk.__init__(self, *args, **kwargs)
container = tk.Frame(self)
#container.pack(side='top', fill='both', expand=True) # (*** 1 ***) this line was commented out
container.grid(column=0, row=0)
# container.grid_rowconfigure(0, weight=1)
# container.grid_columnconfigure(0, weight=1)
self.frames = {}
for shown in tasks:
shown_name = shown.__name__
frame = shown(parent=container, controller=self)
self.frames[shown_name] = frame
frame.grid(row='0', column='0',sticky='snew')
self.rowconfigure(0, weight=1) # (*** 2 ***) this line was added
self.columnconfigure(0, weight=1) # (*** 3 ***) this line was added
self.show_frame('Home')
def show_frame(self, shown_name):
frame = self.frames[shown_name]
frame.tkraise()
class Home(tk.Frame):
def __init__(self, parent ,controller, **kw):
super().__init__(**kw)
self.controller = controller
self.buttons()
def buttons(self):
tasks = [Zad1, Zad2, Zad3, Zad4, Zad5, Zad6, Zad7]
i = 1
r = 0
for page in tasks:
page_name = page.__name__
button = tk.Button(self,
text=i, width=10,
command=lambda page_name=page_name:
self.controller.show_frame(page_name)).grid(row=r,column=0,pady=5)
self.rowconfigure(r, weight=1) # (*** 4 ***) this line was added
i+=1
r+=1
self.columnconfigure(0, weight=1) # (*** 5 ***) this line was added
class Zad1(tk.Frame):
def __init__(self, parent, controller, **kw):
super().__init__(**kw)
self.controller = controller
text = tk.IntVar()
label = tk.Label(self, text="brutto:")
brutto_input = tk.Entry(self, textvariable=text)
show = tk.Button(self, text="Show",width=10, command=lambda: self.netto(text))
home = tk.Button(self, text="Home",width=10, command=lambda: controller.show_frame('Home'))
label.grid(row=0,column=0)
brutto_input.grid(row=0,column=1)
show.grid(row=0,column=2)
home.grid(row=1,column=2)
def netto(self, text):
self.text = text
text_get = text.get()
netto_output = tk.Label(self, height=2, width=15, text=f'Sum: {text_get+1}')
netto_output.grid(row=0,column=3)
netto_output.config(text=f'Sum: {text_get+1}')
class Zad2(tk.Frame):
def __init__(self, parent, controller, **kw):
super().__init__(**kw)
self.controller = controller
label = tk.Label(self, text="2:")
class Zad3(tk.Frame):
def __init__(self, parent, controller, **kw):
super().__init__(**kw)
self.controller = controller
label = tk.Label(self, text="3:")
class Zad4(tk.Frame):
def __init__(self, parent, controller, **kw):
super().__init__(**kw)
self.controller = controller
label = tk.Label(self, text="4:")
class Zad5(tk.Frame):
def __init__(self, parent, controller, **kw):
super().__init__(**kw)
self.controller = controller
label = tk.Label(self, text="5:")
class Zad6(tk.Frame):
def __init__(self, parent, controller, **kw):
super().__init__(**kw)
self.controller = controller
label = tk.Label(self, text="6:")
class Zad7(tk.Frame):
def __init__(self, parent, controller, **kw):
super().__init__(**kw)
self.controller = controller
label = tk.Label(self, text="8:")
if __name__ == "__main__":
app = Main()
app.title('Last Test')
app.geometry('500x500')
app.mainloop()
Assuming you want to use grid, try this:
button.grid(row = 1, col = 0)
If this answer helped, kindly upvote and mark as correct. Appreciate it!
I have shortened my code to 100 lines but in a much larger file I cannot properly use the config function to change a label in a different class. If you run my file here you will see I am attempting to change 0 to a different str value.
from tkinter import *
class App(Tk):'
def __init__(self, *args, **kwargs):
#declare our frame
Tk.__init__(self, *args, **kwargs)
#Setup the frame
container = Frame(self)
container.pack(side = "top", fill = "both", expand = True)
container.grid_rowconfigure(0, weight = 1)
container.grid_columnconfigure(0, weight = 1)
#declare the object to hold the frames
self.frames = {}
#loop through the list of frames
for i in (Page_One, Page_Two):
frame = i(container, self)
self.frames[i] = frame
frame.grid(row = 0, column = 0, sticky = "nsew")
#start with the home page
self.show_frame(Page_One)
def show_frame(self, context):
'''This method will show the frame that is passed through it. \n
Used in button by implementing lamda'''
frame = self.frames[context]
frame.tkraise()
class Page_One(Frame):
def __init__(self, parent, controller):
Frame.__init__(self, parent)
def change_value(value):
Page_Two.label.config(text = value)
print(value)
Button(self, text='Set value to 1',command=lambda *args: change_value('1')).pack()
Button(self, text='Set value to 2',command=lambda *args: change_value('2')).pack()
Button(self, text='Set value to 3',command=lambda *args: change_value('3')).pack()
Button(self, text='Page 2', command = lambda: controller.show_frame(Page_Two)).pack()
class Page_Two(Frame):
def __init__(self, parent, controller):
Frame.__init__(self, parent)
label = Label(self, text = '0')
label.pack()
Button(self, text='Page 1',command = lambda: controller.show_frame(Page_One)).pack()
app = App()
app.mainloop()
Unable to change change frames after a delay
How can I use the showFrames method from logoPage class in the code provided below?
class temp(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
tk.Tk.wm_title(self, "SOC_Workups")
container_ = tk.Frame(self)
container_.pack(side = "top", fill = "both", expand = "True")
container_.grid_rowconfigure(0, weight = 1)
container_.grid_columnconfigure(0, weight = 1)
self.frames = {}
for F in (logoPage, mainPage):
page_name = F.__name__
frame = F(parent=container_, controller=self)
self.frames[page_name] = frame
# put all of the pages in the same location;
# the one on the top of the stacking order
# will be the one that is visible.
frame.grid(row=0, column=0, sticky="nsew")
self.showFrame("logoPage")
def showFrame(self, page):
frame = self.frames[page]
frame.tkraise()
class logoPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
#label = tk.Label(self, text = "Start Page", font = LARGE_FONT)
#label.pack(pady = 10, padx = 10)
canvas = tk.Canvas(self, height=500, width=1000)
canvas.pack()
logo = tk.PhotoImage(file='logo1.png')
background_label = tk.Label(self, image=logo)
background_label.image = logo
background_label.place(relwidth=1, relheight=1)
#parent.showFrame("mainPage")
class mainPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
canvas = tk.Canvas(self, height=500, width=1000)
canvas.pack()
app = temp()
app.mainloop()
The app should start with logoPage and then changes to mainPage after 10 secs. I am unable to call the showFrame method from logoPage class.
You have access to showPage() using controller
controller.showFrame("mainPage")
To run with delay you can use root.after(millisecond, callback). In your code it will need lambda to create callback which runs function with arguments`
parent.after(10000, lambda:controller.showFrame("mainPage"))
EDIT: As Bryan Oakley said in comment you can run it without lambda
parent.after(10000, controller.showFrame, "mainPage")
Second argument in after() is callback - function's name without () - and all its arguments are as next arguments in after()
I changed it in code below too.
Full working code:
There is good rule in PEP 8 -- Style Guide for Python Code which suggests to use CamelCaseNames for classes - ie. LogoPage, MainPage, Temp similar to Button, Frame, Button, etc. it helps to recognize class in code. And some tools use speciala color for classes - see colors in code below.
import tkinter as tk
class Temp(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
tk.Tk.wm_title(self, "SOC_Workups")
container_ = tk.Frame(self)
container_.pack(side = "top", fill = "both", expand = "True")
container_.grid_rowconfigure(0, weight = 1)
container_.grid_columnconfigure(0, weight = 1)
self.frames = {}
for F in (LogoPage, MainPage):
page_name = F.__name__
frame = F(parent=container_, controller=self)
self.frames[page_name] = frame
# put all of the pages in the same location;
# the one on the top of the stacking order
# will be the one that is visible.
frame.grid(row=0, column=0, sticky="nsew")
print(self.frames)
self.showFrame("LogoPage")
def showFrame(self, page):
frame = self.frames[page]
frame.tkraise()
class LogoPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
#label = tk.Label(self, text = "Start Page", font = LARGE_FONT)
#label.pack(pady = 10, padx = 10)
canvas = tk.Canvas(self, height=500, width=1000)
canvas.pack()
logo = tk.PhotoImage(file='logo1.png')
background_label = tk.Label(self, image=logo)
background_label.image = logo
background_label.place(relwidth=1, relheight=1)
#parent.showFrame("mainPage")
#parent.after(10000, lambda:controller.showFrame("MainPage"))
parent.after(10000, controller.showFrame, "MainPage")
class MainPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
canvas = tk.Canvas(self, height=500, width=1000)
canvas.pack()
app = Temp()
app.mainloop()
I'm attempting to write a program in tkinter where the user clicks on a button with their name and a verification page shows it to them. The problem I'm having is that the variable is either resetting or I'm accessing it wrong:
import tkinter as tk
from tkinter import *
from tkinter import ttk
LARGE_FONT = ("Times New Roman", 12)
NORM_FONT = ("Times New Roman", 10)
root = Tk()
root.withdraw()
class DIS(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
tk.Tk.iconbitmap(self, default="")
tk.Tk.wm_title(self, "program")
container = tk.Frame(self)
container.pack(side="top", fill="both", expand = True)
container.grid_rowconfigure(0, weight = 1)
container.grid_columnconfigure(0, weight = 1)
self.frames = {}
for F in (StartPage, contactQues, nameVerify):
frame = F(container, self)
self.frames[F] = frame
frame.grid(row = 0, column = 0, sticky = "nsew")
self.show_frame(StartPage)
def show_frame(self, cont):
frame = self.frames[cont]
frame.tkraise()
class StartPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
button2 = ttk.Button(self, text = "Name Select",
command=lambda: controller.show_frame(contactQues))
button2.pack()
class contactQues(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.controller = controller
global name
name = StringVar()
label1 = tk.Label(self, text = "Select Your Name", font = LARGE_FONT)
label1.pack(pady=10, padx=10)
button2 = ttk.Button(self, text = "Bojangles", command = self.bojangles)
button2.pack(pady=5)
def bojangles(self):
name.set("Mr. Bojangles")
self.controller.show_frame(nameVerify)
#
#Many other names to select
#
class nameVerify(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.controller = controller
namename = name.get()
label5 = tk.Label(self, text = "Your Name:", font = LARGE_FONT)
label5.pack(pady=10, padx=10)
labelcontact = tk.Label(self, text = namename, font = NORM_FONT)
labelcontact.pack()
app = DIS()
app.mainloop()
So, in essence, what I want to happen is:
- Program runs and user presses "Name select", user selects their name, and the final page shows their selection.
I've tried messing with globals, textvariables for the labelcontact label, StringVar(), etc. and can't seem to peg this one down.
Is there a better way to do this? Or am I doing something inherently wrong?
Thank you for any help.
I suggest making name an attribute of the DIS class. Then your StartPage and nameVerify instances can access it via their controller attributes. If you want labelcontact to update automatically whenever name does, use the textvariable attribute.
Additionally, you need to delete your root = Tk() and root.withdraw() lines. I don't know why, but as long as they're there, the labelcontact Label won't properly update. They don't appear to do anything in any case - hopefully they aren't crucial to your actual code.
import tkinter as tk
from tkinter import *
from tkinter import ttk
LARGE_FONT = ("Times New Roman", 12)
NORM_FONT = ("Times New Roman", 10)
class DIS(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
tk.Tk.iconbitmap(self, default="")
tk.Tk.wm_title(self, "program")
container = tk.Frame(self)
container.pack(side="top", fill="both", expand = True)
container.grid_rowconfigure(0, weight = 1)
container.grid_columnconfigure(0, weight = 1)
self.name = StringVar()
self.frames = {}
for F in (StartPage, contactQues, nameVerify):
frame = F(container, self)
self.frames[F] = frame
frame.grid(row = 0, column = 0, sticky = "nsew")
self.show_frame(StartPage)
def show_frame(self, cont):
frame = self.frames[cont]
frame.tkraise()
class StartPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
button2 = ttk.Button(self, text = "Name Select",
command=lambda: controller.show_frame(contactQues))
button2.pack()
class contactQues(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.controller = controller
label1 = tk.Label(self, text = "Select Your Name", font = LARGE_FONT)
label1.pack(pady=10, padx=10)
button2 = ttk.Button(self, text = "Bojangles", command = self.bojangles)
button2.pack(pady=5)
def bojangles(self):
self.controller.name.set("Mr. Bojangles")
self.controller.show_frame(nameVerify)
#
#Many other names to select
#
class nameVerify(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.controller = controller
label5 = tk.Label(self, text = "Your Name:", font = LARGE_FONT)
label5.pack(pady=10, padx=10)
labelcontact = tk.Label(self, textvariable = self.controller.name, font = NORM_FONT)
labelcontact.pack()
app = DIS()
app.mainloop()
I am writing a GUI code that opens frame using Tkinter. I referred various websites. Now while testing I am facing problem. For example:
In 1st frame, I select MainController button.
in 2nd frame press MC_CONFIG button.
in 3rd frame I set XML PATH then clicked MC SYSTEM.xml button
If I go to Back to Home button and follow the same procedure, MC_CONFIG button gets disabled (i.e I cannot go further).
If I comment(delete) this line(126)
tk.Frame.__init__(self)
in method def nacxml(self): of class MC_CONFIG, it is working perfectly.
The below one is just part of my main code bu facing problem here.
Please guide me.
import Tkinter as tk
import xml.dom.minidom
from Tkinter import *
import tkMessageBox
from array import *
import tkFileDialog
import os
LARGE_FONT= ("Verdana", 12)
class SeaofBTCapp(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
tk.Tk.wm_title(self, "Switch Installer window")
container = tk.Frame(self)
container.pack(side="top", fill="both", expand = True)
container.grid_rowconfigure(0, weight=1)
container.grid_columnconfigure(0, weight=1)
self.frames = {}
#for F in (StartPage, PageOne, PageTwo):
for F in (StartPage, MainController,xmlpath,MC_CONFIG):
frame = F(container,self)
self.frames[F] = frame
frame.grid(row=0, column=0, sticky="nsew")
self.show_frame(StartPage)
def show_frame(self, cont):
frame = self.frames[cont]
frame.tkraise()
class StartPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.modules_label = ['MAINCONTROLLER']
self.modules_function = [MainController]
self.modules_label_index = len(self.modules_label)
self.modules_function_index = len(self.modules_function)
print("self.modules_label_index = %s" %self.modules_label_index)
label = Label(self, text="SWITCH INSTALLER", font=LARGE_FONT)
label.pack(pady=10,padx=10)
#button = Button(self, text="Visit Page 1",
button3 = Button(self, text="SELECT",
command=lambda: controller.show_frame(MainController))
button3.pack()
label3 = Label(self, text="MainController", font = LARGE_FONT)
label3.place(x= 50, y=100+10)
button8 = Button(self, text="Quit", command=self.quit)
button8.pack()
class xmlpath(tk.Frame):
#xfilename="+++"
def __init__(self, parent, controller):
self.xfilename="srinivasan"
tk.Frame.__init__(self, parent)
label = Label(self, text="Page One!!!", font=LARGE_FONT)
label.pack(pady=10,padx=10)
button1 = Button(self, text="Back to Home",
command=lambda: controller.show_frame(StartPage))
button1.pack()
button1 = Button(self, text="XML PATH",
command=self.newxmlpath)
button1.pack()
def newxmlpath(self,*args):
# ObjNAC= NacHandler()
self.filename = tkFileDialog.askopenfilename()
print(self.filename)
#ObjNAC.temp_method(self,self.filename)
return self.filename
class MainController(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
label = Label(self, text="|--Frame1 MainController --|", font = LARGE_FONT)
label.pack(pady=10,padx=10)
mc_button1 = Button(self, text="MC_CONFIG", command = lambda: controller.show_frame(MC_CONFIG))
mc_button1.pack()
mc_button2 = Button(self, text="MENU HOME", command = lambda: controller.show_frame(StartPage))
mc_button2.pack()
self.pack (fill = BOTH, expand = 1)
class MC_CONFIG(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
print "Inside MC_CONFIG"
self.database = []
# set root as parent
self.parent = parent
label1 = Label(self, text="|------------Frame2--MainController---------------|", font=LARGE_FONT)
label1.pack(pady = 10,padx = 10)
label2 = Label(self, text="Edit SYSTEM.xml File", font=LARGE_FONT)
label2.pack(pady = 10,padx = 10)
button1 = Button(self, text="XML PATH",
command=self.newxmlpath)
button1.pack(pady = 10,padx = 10)
button2 = Button(self, text = "MC SYSTEM.xml", command = self.nacxml)
button2.pack(pady = 10,padx = 10)
button3 = Button(self, text="Back to Home",
command=lambda: controller.show_frame(StartPage))
button3.pack(pady = 10,padx = 10)
def newxmlpath(self, *args):
self.filename = tkFileDialog.askopenfilename()
print(self.filename)
return self.filename
def nacxml(self):
tk.Frame.__init__(self)
print "===Inside Nacxml1==="
app = SeaofBTCapp()
app.geometry ("640x480+300+300")
app.mainloop()
The problem is this:
def nacxml(self):
tk.Frame.__init__(self)
You should only ever call the superclass constructor from within the constructor of the subclass. Doing so anywhere else will certainly not do whatever you think it's doing.
Finally code is working as intended
1.Deleted tk.Frame.__init__(self) as indicated by Bryan Oakley
def nacxml(self):
tk.Frame.__init__(self)
2.Controller is not intialized in self.But it is present in def __init__(self, parent, controller):.Hence added
self.controller = controller in MC_CONFIG class
class MC_CONFIG(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
print "Inside MC_CONFIG"
So can be used in below method as self.controller.show_frame(StartPage)
def nacxml(self):
tk.Frame.__init__(self)
print "===Inside Nacxml1==="
# xml read,edit,widget code will be added here
self.controller.show_frame(StartPage)