How to call a tkinter class as an object or in button click?Switch between two frames in tkinterHow to know if an object has an attribute in PythonPython class inherits objectttk tkinter multiple frames/windowsSimple Inherit from class in Python throws errorTkinter button calling method on an objectTkInter Frame doesn't load if another function is calledhow to implement mvc with tkinter in pythonHow to Update Label text in tkinter subframe classHow to check the text of a tkinter Button?deconstructing basic Tk inter script

Inverter Power draw from 12V battery

How to extract lower and upper bound in numeric format from a confidence interval string?

How feasible is the Delta-Glider?

Tic-Tac-Toe for the terminal

File globbing pattern, !(*example), behaves differently in bash script than it does in bash shell

Windows 10 Programs start without visual Interface

Why does the 6502 have the BIT instruction?

Why did this prime-sequence puzzle not work?

Plot exactly N bounce of a ball

How do I subvert the tropes of a train heist?

Is a post-climate apocolypse city in which many or most insects have disappeared realistic?

What is the 中 in ダウンロード中?

Why colon to denote that a value belongs to a type?

Can non-English-speaking characters use wordplay specific to English?

Do firearms count as ranged weapons?

Could I be denied entry into Ireland due to medical and police situations during a previous UK visit?

Modern approach to radio buttons

Can a non-EU citizen travel within schengen zone freely without passport?

shutdown at specific date

Why do Russians call their women expensive ("дорогая")?

Leading and Suffering Numbers

Why doesn't the Earth's acceleration towards the Moon accumulate to push the Earth off its orbit?

What does it mean when you think without speaking?

Where did the “Vikings wear helmets with horn” stereotype come from and why?



How to call a tkinter class as an object or in button click?


Switch between two frames in tkinterHow to know if an object has an attribute in PythonPython class inherits objectttk tkinter multiple frames/windowsSimple Inherit from class in Python throws errorTkinter button calling method on an objectTkInter Frame doesn't load if another function is calledhow to implement mvc with tkinter in pythonHow to Update Label text in tkinter subframe classHow to check the text of a tkinter Button?deconstructing basic Tk inter script






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








0















I'll be short. I created a tkinter GUI, which I need to call as an object or a window that appears on button click.
code is:



class Test(Frame):

def __init__(self,master = None):
Frame.__init__(self, master)
self.master = master
self.win()

def win(self):
self.pack()

self.label = Label(self, text="hello World!").pack()
self.quit = Button(self, text= "quit", command = self.master.destroy).pack()


the code works fine when I call the class in the same file. ie, by adding



root=Tk()
Test()


but I want it to be called at a button click, but outside in other gui.



what I tried:



1) applying the same root = Tk() as it is.



2) calling it in a class as object by: self.test = Test() and applying command self.test.win in the button.



Problem:'module' object is not callable.



code of other gui, where I want the button to call Test class and show gui of that class:



import Test
class Wtf(Frame):
def __init__(self, master=None):
Frame.__init__(self, master)
self.master = master
self.btn()
self.test = Test()

def btn(self):
self.pack()

self.test = Test()

self.btn = Button(self, text = "Call", command = self.test.win).pack()

root=Tk()
app = Wtf(root)


Thanks in advance. Hope I defined as much as required.



for those who did not understand:all i'm trying to here is to link the class data to the button 'btn', so that when I press the button I could get the class Test gui displayed either in the same window root or a different window.



please note: i'm a newbie at python and this program might not make sence to you, but all i'm trying here is to call the class Test on a buttonclick of 'btn'.










share|improve this question
























  • Button(self, text = "Call", command = self.test.win).pack() returns None, so that's what is assigned to self.quit or self.btn.

    – martineau
    Mar 24 at 8:54











  • i'm a newbie at python so all I was trying to do was displaying the gui of class test in the button btn

    – Andrious Prime
    Mar 24 at 12:44











  • My answer does that.

    – martineau
    Mar 24 at 12:49

















0















I'll be short. I created a tkinter GUI, which I need to call as an object or a window that appears on button click.
code is:



class Test(Frame):

def __init__(self,master = None):
Frame.__init__(self, master)
self.master = master
self.win()

def win(self):
self.pack()

self.label = Label(self, text="hello World!").pack()
self.quit = Button(self, text= "quit", command = self.master.destroy).pack()


the code works fine when I call the class in the same file. ie, by adding



root=Tk()
Test()


but I want it to be called at a button click, but outside in other gui.



what I tried:



1) applying the same root = Tk() as it is.



2) calling it in a class as object by: self.test = Test() and applying command self.test.win in the button.



Problem:'module' object is not callable.



code of other gui, where I want the button to call Test class and show gui of that class:



import Test
class Wtf(Frame):
def __init__(self, master=None):
Frame.__init__(self, master)
self.master = master
self.btn()
self.test = Test()

def btn(self):
self.pack()

self.test = Test()

self.btn = Button(self, text = "Call", command = self.test.win).pack()

root=Tk()
app = Wtf(root)


Thanks in advance. Hope I defined as much as required.



for those who did not understand:all i'm trying to here is to link the class data to the button 'btn', so that when I press the button I could get the class Test gui displayed either in the same window root or a different window.



please note: i'm a newbie at python and this program might not make sence to you, but all i'm trying here is to call the class Test on a buttonclick of 'btn'.










share|improve this question
























  • Button(self, text = "Call", command = self.test.win).pack() returns None, so that's what is assigned to self.quit or self.btn.

    – martineau
    Mar 24 at 8:54











  • i'm a newbie at python so all I was trying to do was displaying the gui of class test in the button btn

    – Andrious Prime
    Mar 24 at 12:44











  • My answer does that.

    – martineau
    Mar 24 at 12:49













0












0








0








I'll be short. I created a tkinter GUI, which I need to call as an object or a window that appears on button click.
code is:



class Test(Frame):

def __init__(self,master = None):
Frame.__init__(self, master)
self.master = master
self.win()

def win(self):
self.pack()

self.label = Label(self, text="hello World!").pack()
self.quit = Button(self, text= "quit", command = self.master.destroy).pack()


the code works fine when I call the class in the same file. ie, by adding



root=Tk()
Test()


but I want it to be called at a button click, but outside in other gui.



what I tried:



1) applying the same root = Tk() as it is.



2) calling it in a class as object by: self.test = Test() and applying command self.test.win in the button.



Problem:'module' object is not callable.



code of other gui, where I want the button to call Test class and show gui of that class:



import Test
class Wtf(Frame):
def __init__(self, master=None):
Frame.__init__(self, master)
self.master = master
self.btn()
self.test = Test()

def btn(self):
self.pack()

self.test = Test()

self.btn = Button(self, text = "Call", command = self.test.win).pack()

root=Tk()
app = Wtf(root)


Thanks in advance. Hope I defined as much as required.



for those who did not understand:all i'm trying to here is to link the class data to the button 'btn', so that when I press the button I could get the class Test gui displayed either in the same window root or a different window.



please note: i'm a newbie at python and this program might not make sence to you, but all i'm trying here is to call the class Test on a buttonclick of 'btn'.










share|improve this question
















I'll be short. I created a tkinter GUI, which I need to call as an object or a window that appears on button click.
code is:



class Test(Frame):

def __init__(self,master = None):
Frame.__init__(self, master)
self.master = master
self.win()

def win(self):
self.pack()

self.label = Label(self, text="hello World!").pack()
self.quit = Button(self, text= "quit", command = self.master.destroy).pack()


the code works fine when I call the class in the same file. ie, by adding



root=Tk()
Test()


but I want it to be called at a button click, but outside in other gui.



what I tried:



1) applying the same root = Tk() as it is.



2) calling it in a class as object by: self.test = Test() and applying command self.test.win in the button.



Problem:'module' object is not callable.



code of other gui, where I want the button to call Test class and show gui of that class:



import Test
class Wtf(Frame):
def __init__(self, master=None):
Frame.__init__(self, master)
self.master = master
self.btn()
self.test = Test()

def btn(self):
self.pack()

self.test = Test()

self.btn = Button(self, text = "Call", command = self.test.win).pack()

root=Tk()
app = Wtf(root)


Thanks in advance. Hope I defined as much as required.



for those who did not understand:all i'm trying to here is to link the class data to the button 'btn', so that when I press the button I could get the class Test gui displayed either in the same window root or a different window.



please note: i'm a newbie at python and this program might not make sence to you, but all i'm trying here is to call the class Test on a buttonclick of 'btn'.







python python-3.x tkinter






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 24 at 12:47







Andrious Prime

















asked Mar 24 at 8:39









Andrious PrimeAndrious Prime

44




44












  • Button(self, text = "Call", command = self.test.win).pack() returns None, so that's what is assigned to self.quit or self.btn.

    – martineau
    Mar 24 at 8:54











  • i'm a newbie at python so all I was trying to do was displaying the gui of class test in the button btn

    – Andrious Prime
    Mar 24 at 12:44











  • My answer does that.

    – martineau
    Mar 24 at 12:49

















  • Button(self, text = "Call", command = self.test.win).pack() returns None, so that's what is assigned to self.quit or self.btn.

    – martineau
    Mar 24 at 8:54











  • i'm a newbie at python so all I was trying to do was displaying the gui of class test in the button btn

    – Andrious Prime
    Mar 24 at 12:44











  • My answer does that.

    – martineau
    Mar 24 at 12:49
















Button(self, text = "Call", command = self.test.win).pack() returns None, so that's what is assigned to self.quit or self.btn.

– martineau
Mar 24 at 8:54





Button(self, text = "Call", command = self.test.win).pack() returns None, so that's what is assigned to self.quit or self.btn.

– martineau
Mar 24 at 8:54













i'm a newbie at python so all I was trying to do was displaying the gui of class test in the button btn

– Andrious Prime
Mar 24 at 12:44





i'm a newbie at python so all I was trying to do was displaying the gui of class test in the button btn

– Andrious Prime
Mar 24 at 12:44













My answer does that.

– martineau
Mar 24 at 12:49





My answer does that.

– martineau
Mar 24 at 12:49












3 Answers
3






active

oldest

votes


















0














I think I know, in this version you can call a function in Test() class from the main class of the program.



After run the script click on Open button then click on Call and watch what happen.



import tkinter as tk
from tkinter import messagebox

class Test(tk.Toplevel):

def __init__(self, parent):
super().__init__()

self.parent = parent
self.title("I'm a new toplevel.")
self.init_ui()

def init_ui(self):

self.label = tk.Label(self, text=self.title()).pack()
self.close_me = tk.Button(self, text= "Close me", command = self.on_close_me).pack()
self.close_parent = tk.Button(self, text= "Close parent", command = self.on_close_parent).pack()

def on_close_me(self):
self.destroy()

def on_close_parent(self):
self.parent.on_close()

def callback(self):
msg = "I come from parent toplevel"
messagebox.showwarning(self.master.title(), msg, parent=self)




class App(tk.Frame):

def __init__(self,):

super().__init__()

self.master.title("Hello World")

self.obj = None

self.init_ui()

def init_ui(self):

self.pack(fill=tk.BOTH, expand=1,)

f = tk.Frame()

w = tk.Frame()

tk.Button(w, text="Open", command=self.callback).pack()
tk.Button(w, text="Call", command=self.call_child_function).pack()
tk.Button(w, text="Close", command=self.on_close).pack()

f.pack(side=tk.LEFT, fill=tk.BOTH, expand=0)
w.pack(side=tk.RIGHT, fill=tk.BOTH, expand=0)

def callback(self):
self.obj = Test(self)

def call_child_function(self):
self.obj.callback()


def on_close(self,evt=None):
self.master.destroy()

if __name__ == '__main__':
app = App()
app.mainloop()





share|improve this answer























  • wow! it does work as per I request but this would be taking a hell lot time to understand. by the way thanks for the help.

    – Andrious Prime
    Mar 24 at 13:08



















0














I don't understand exactly what you're trying to accomplish, but there are numerous problems with your code, some of which I already pointed-out in a comment.



The 'module' object is not callable problem is because you have a module called Test which defines a class named Test inside it, so the problem can be avoided by using Test.Test when referring to the class.



Here are working versions of the main script and the Test.py module:



main.py:



from tkinter import *
import Test


class Wtf(Frame):
def __init__(self, master=None):
Frame.__init__(self, master)
self.master = master
self.pack()
self.create_widgets()

def create_widgets(self):
self.test = Test.Test(self.master)
self.btn = Button(self, text="Call", command=self.test.win)
self.btn.pack()

root = k()
app = Wtf(root)
app.mainloop()



Test.py:



from tkinter import *

class Test(Frame):

def __init__(self, master=None):
Frame.__init__(self, master)
self.master = master
self.pack()

def win(self):
self.label = Label(self, text="Hello World!")
self.label.pack()
self.quit = Button(self, text= "quit", command=self.master.destroy)
self.quit.pack()



Update



Based on your comment about what you really want to do, I've adapted the accepted answer to the question Switch between two frames in tkinter to your needs:



main.py



import tkinter as tk
import Test


class App(tk.Tk):

def __init__(self, *args, **kwargs):
super().__init__(*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 (Wtf, Test.Test):
page_name = F.__name__
frame = F(parent=container, controller=self)
self.frames[page_name] = frame
frame.grid(row=0, column=0, sticky="nsew")

self.show_frame("Wtf")

def show_frame(self, page_name):
""" Show a frame for the given page name. """
frame = self.frames[page_name]
frame.tkraise()


class Wtf(tk.Frame):

def __init__(self, parent, controller):
super().__init__(parent)
self.parent = parent
self.controller = controller
self.create_widgets()

def create_widgets(self):
self.test = Test.Test(self.parent, self.controller)
self.btn = tk.Button(self, text="Call",
command=lambda: self.controller.show_frame("Test"))
self.btn.pack()


if __name__ == "__main__":

app = App()
app.mainloop()



Test.py:



import tkinter as tk


class Test(tk.Frame):

def __init__(self, parent, controller):
super().__init__(parent)
self.parent = parent
self.controller = controller
self.create_widgets()

def create_widgets(self):
self.label = tk.Label(self, text="Hello World!")
self.label.pack()
self.quit = tk.Button(self, text= "Quit", command=self.controller.destroy)
self.quit.pack()





share|improve this answer

























  • sir ! it is a great program. I did understand it easily. I have changed it to control various buttons and provide more than 1 GUIs. the problem i'm occurring with it is that i'm no able to reverse it. to explain it more specifically, in this case the flow is from main to test but I want to add a back button in test which can take me back from test to main without exiting the main window.

    – Andrious Prime
    Apr 15 at 8:40











  • Andrious: If you found my answer useful, please consider at least up-voting it. See What should I do when someone answers my question? Afterwards I'll probably be more inclined to answer follow-on questions you might have...

    – martineau
    Apr 15 at 14:48


















-1














I am not sure to have understood well the question, anyway look below.



Particularly to




self.close_me = tk.Button(self, text= "Close me", command = self.on_close_me).pack()



self.close_parent = tk.Button(self, text= "Close parent", command = self.on_close_parent).pack()




You can even put the class Test on another file and input it.



import tkinter as tk

class Test(tk.Toplevel):

def __init__(self, parent):
super().__init__()

self.parent = parent
self.title("I'm a new toplevel.")
self.init_ui()

def init_ui(self):

self.label = tk.Label(self, text=self.title()).pack()
self.close_me = tk.Button(self, text= "Close me", command = self.on_close_me).pack()
self.close_parent = tk.Button(self, text= "Close parent", command = self.on_close_parent).pack()

def on_close_me(self):
self.destroy()

def on_close_parent(self):
self.parent.on_close()


class App(tk.Frame):

def __init__(self,):

super().__init__()

self.master.title("Hello World")

self.init_ui()

def init_ui(self):

self.pack(fill=tk.BOTH, expand=1,)

f = tk.Frame()

w = tk.Frame()

tk.Button(w, text="Open", command=self.callback).pack()
tk.Button(w, text="Close", command=self.on_close).pack()

f.pack(side=tk.LEFT, fill=tk.BOTH, expand=0)
w.pack(side=tk.RIGHT, fill=tk.BOTH, expand=0)


def callback(self):
obj = Test(self)

def on_close(self,evt=None):
self.master.destroy()

if __name__ == '__main__':
app = App()
app.mainloop()





share|improve this answer























  • .pack() returns None.

    – martineau
    Mar 24 at 9:00











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55321997%2fhow-to-call-a-tkinter-class-as-an-object-or-in-button-click%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























3 Answers
3






active

oldest

votes








3 Answers
3






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














I think I know, in this version you can call a function in Test() class from the main class of the program.



After run the script click on Open button then click on Call and watch what happen.



import tkinter as tk
from tkinter import messagebox

class Test(tk.Toplevel):

def __init__(self, parent):
super().__init__()

self.parent = parent
self.title("I'm a new toplevel.")
self.init_ui()

def init_ui(self):

self.label = tk.Label(self, text=self.title()).pack()
self.close_me = tk.Button(self, text= "Close me", command = self.on_close_me).pack()
self.close_parent = tk.Button(self, text= "Close parent", command = self.on_close_parent).pack()

def on_close_me(self):
self.destroy()

def on_close_parent(self):
self.parent.on_close()

def callback(self):
msg = "I come from parent toplevel"
messagebox.showwarning(self.master.title(), msg, parent=self)




class App(tk.Frame):

def __init__(self,):

super().__init__()

self.master.title("Hello World")

self.obj = None

self.init_ui()

def init_ui(self):

self.pack(fill=tk.BOTH, expand=1,)

f = tk.Frame()

w = tk.Frame()

tk.Button(w, text="Open", command=self.callback).pack()
tk.Button(w, text="Call", command=self.call_child_function).pack()
tk.Button(w, text="Close", command=self.on_close).pack()

f.pack(side=tk.LEFT, fill=tk.BOTH, expand=0)
w.pack(side=tk.RIGHT, fill=tk.BOTH, expand=0)

def callback(self):
self.obj = Test(self)

def call_child_function(self):
self.obj.callback()


def on_close(self,evt=None):
self.master.destroy()

if __name__ == '__main__':
app = App()
app.mainloop()





share|improve this answer























  • wow! it does work as per I request but this would be taking a hell lot time to understand. by the way thanks for the help.

    – Andrious Prime
    Mar 24 at 13:08
















0














I think I know, in this version you can call a function in Test() class from the main class of the program.



After run the script click on Open button then click on Call and watch what happen.



import tkinter as tk
from tkinter import messagebox

class Test(tk.Toplevel):

def __init__(self, parent):
super().__init__()

self.parent = parent
self.title("I'm a new toplevel.")
self.init_ui()

def init_ui(self):

self.label = tk.Label(self, text=self.title()).pack()
self.close_me = tk.Button(self, text= "Close me", command = self.on_close_me).pack()
self.close_parent = tk.Button(self, text= "Close parent", command = self.on_close_parent).pack()

def on_close_me(self):
self.destroy()

def on_close_parent(self):
self.parent.on_close()

def callback(self):
msg = "I come from parent toplevel"
messagebox.showwarning(self.master.title(), msg, parent=self)




class App(tk.Frame):

def __init__(self,):

super().__init__()

self.master.title("Hello World")

self.obj = None

self.init_ui()

def init_ui(self):

self.pack(fill=tk.BOTH, expand=1,)

f = tk.Frame()

w = tk.Frame()

tk.Button(w, text="Open", command=self.callback).pack()
tk.Button(w, text="Call", command=self.call_child_function).pack()
tk.Button(w, text="Close", command=self.on_close).pack()

f.pack(side=tk.LEFT, fill=tk.BOTH, expand=0)
w.pack(side=tk.RIGHT, fill=tk.BOTH, expand=0)

def callback(self):
self.obj = Test(self)

def call_child_function(self):
self.obj.callback()


def on_close(self,evt=None):
self.master.destroy()

if __name__ == '__main__':
app = App()
app.mainloop()





share|improve this answer























  • wow! it does work as per I request but this would be taking a hell lot time to understand. by the way thanks for the help.

    – Andrious Prime
    Mar 24 at 13:08














0












0








0







I think I know, in this version you can call a function in Test() class from the main class of the program.



After run the script click on Open button then click on Call and watch what happen.



import tkinter as tk
from tkinter import messagebox

class Test(tk.Toplevel):

def __init__(self, parent):
super().__init__()

self.parent = parent
self.title("I'm a new toplevel.")
self.init_ui()

def init_ui(self):

self.label = tk.Label(self, text=self.title()).pack()
self.close_me = tk.Button(self, text= "Close me", command = self.on_close_me).pack()
self.close_parent = tk.Button(self, text= "Close parent", command = self.on_close_parent).pack()

def on_close_me(self):
self.destroy()

def on_close_parent(self):
self.parent.on_close()

def callback(self):
msg = "I come from parent toplevel"
messagebox.showwarning(self.master.title(), msg, parent=self)




class App(tk.Frame):

def __init__(self,):

super().__init__()

self.master.title("Hello World")

self.obj = None

self.init_ui()

def init_ui(self):

self.pack(fill=tk.BOTH, expand=1,)

f = tk.Frame()

w = tk.Frame()

tk.Button(w, text="Open", command=self.callback).pack()
tk.Button(w, text="Call", command=self.call_child_function).pack()
tk.Button(w, text="Close", command=self.on_close).pack()

f.pack(side=tk.LEFT, fill=tk.BOTH, expand=0)
w.pack(side=tk.RIGHT, fill=tk.BOTH, expand=0)

def callback(self):
self.obj = Test(self)

def call_child_function(self):
self.obj.callback()


def on_close(self,evt=None):
self.master.destroy()

if __name__ == '__main__':
app = App()
app.mainloop()





share|improve this answer













I think I know, in this version you can call a function in Test() class from the main class of the program.



After run the script click on Open button then click on Call and watch what happen.



import tkinter as tk
from tkinter import messagebox

class Test(tk.Toplevel):

def __init__(self, parent):
super().__init__()

self.parent = parent
self.title("I'm a new toplevel.")
self.init_ui()

def init_ui(self):

self.label = tk.Label(self, text=self.title()).pack()
self.close_me = tk.Button(self, text= "Close me", command = self.on_close_me).pack()
self.close_parent = tk.Button(self, text= "Close parent", command = self.on_close_parent).pack()

def on_close_me(self):
self.destroy()

def on_close_parent(self):
self.parent.on_close()

def callback(self):
msg = "I come from parent toplevel"
messagebox.showwarning(self.master.title(), msg, parent=self)




class App(tk.Frame):

def __init__(self,):

super().__init__()

self.master.title("Hello World")

self.obj = None

self.init_ui()

def init_ui(self):

self.pack(fill=tk.BOTH, expand=1,)

f = tk.Frame()

w = tk.Frame()

tk.Button(w, text="Open", command=self.callback).pack()
tk.Button(w, text="Call", command=self.call_child_function).pack()
tk.Button(w, text="Close", command=self.on_close).pack()

f.pack(side=tk.LEFT, fill=tk.BOTH, expand=0)
w.pack(side=tk.RIGHT, fill=tk.BOTH, expand=0)

def callback(self):
self.obj = Test(self)

def call_child_function(self):
self.obj.callback()


def on_close(self,evt=None):
self.master.destroy()

if __name__ == '__main__':
app = App()
app.mainloop()






share|improve this answer












share|improve this answer



share|improve this answer










answered Mar 24 at 9:19









1966bc1966bc

2226




2226












  • wow! it does work as per I request but this would be taking a hell lot time to understand. by the way thanks for the help.

    – Andrious Prime
    Mar 24 at 13:08


















  • wow! it does work as per I request but this would be taking a hell lot time to understand. by the way thanks for the help.

    – Andrious Prime
    Mar 24 at 13:08

















wow! it does work as per I request but this would be taking a hell lot time to understand. by the way thanks for the help.

– Andrious Prime
Mar 24 at 13:08






wow! it does work as per I request but this would be taking a hell lot time to understand. by the way thanks for the help.

– Andrious Prime
Mar 24 at 13:08














0














I don't understand exactly what you're trying to accomplish, but there are numerous problems with your code, some of which I already pointed-out in a comment.



The 'module' object is not callable problem is because you have a module called Test which defines a class named Test inside it, so the problem can be avoided by using Test.Test when referring to the class.



Here are working versions of the main script and the Test.py module:



main.py:



from tkinter import *
import Test


class Wtf(Frame):
def __init__(self, master=None):
Frame.__init__(self, master)
self.master = master
self.pack()
self.create_widgets()

def create_widgets(self):
self.test = Test.Test(self.master)
self.btn = Button(self, text="Call", command=self.test.win)
self.btn.pack()

root = k()
app = Wtf(root)
app.mainloop()



Test.py:



from tkinter import *

class Test(Frame):

def __init__(self, master=None):
Frame.__init__(self, master)
self.master = master
self.pack()

def win(self):
self.label = Label(self, text="Hello World!")
self.label.pack()
self.quit = Button(self, text= "quit", command=self.master.destroy)
self.quit.pack()



Update



Based on your comment about what you really want to do, I've adapted the accepted answer to the question Switch between two frames in tkinter to your needs:



main.py



import tkinter as tk
import Test


class App(tk.Tk):

def __init__(self, *args, **kwargs):
super().__init__(*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 (Wtf, Test.Test):
page_name = F.__name__
frame = F(parent=container, controller=self)
self.frames[page_name] = frame
frame.grid(row=0, column=0, sticky="nsew")

self.show_frame("Wtf")

def show_frame(self, page_name):
""" Show a frame for the given page name. """
frame = self.frames[page_name]
frame.tkraise()


class Wtf(tk.Frame):

def __init__(self, parent, controller):
super().__init__(parent)
self.parent = parent
self.controller = controller
self.create_widgets()

def create_widgets(self):
self.test = Test.Test(self.parent, self.controller)
self.btn = tk.Button(self, text="Call",
command=lambda: self.controller.show_frame("Test"))
self.btn.pack()


if __name__ == "__main__":

app = App()
app.mainloop()



Test.py:



import tkinter as tk


class Test(tk.Frame):

def __init__(self, parent, controller):
super().__init__(parent)
self.parent = parent
self.controller = controller
self.create_widgets()

def create_widgets(self):
self.label = tk.Label(self, text="Hello World!")
self.label.pack()
self.quit = tk.Button(self, text= "Quit", command=self.controller.destroy)
self.quit.pack()





share|improve this answer

























  • sir ! it is a great program. I did understand it easily. I have changed it to control various buttons and provide more than 1 GUIs. the problem i'm occurring with it is that i'm no able to reverse it. to explain it more specifically, in this case the flow is from main to test but I want to add a back button in test which can take me back from test to main without exiting the main window.

    – Andrious Prime
    Apr 15 at 8:40











  • Andrious: If you found my answer useful, please consider at least up-voting it. See What should I do when someone answers my question? Afterwards I'll probably be more inclined to answer follow-on questions you might have...

    – martineau
    Apr 15 at 14:48















0














I don't understand exactly what you're trying to accomplish, but there are numerous problems with your code, some of which I already pointed-out in a comment.



The 'module' object is not callable problem is because you have a module called Test which defines a class named Test inside it, so the problem can be avoided by using Test.Test when referring to the class.



Here are working versions of the main script and the Test.py module:



main.py:



from tkinter import *
import Test


class Wtf(Frame):
def __init__(self, master=None):
Frame.__init__(self, master)
self.master = master
self.pack()
self.create_widgets()

def create_widgets(self):
self.test = Test.Test(self.master)
self.btn = Button(self, text="Call", command=self.test.win)
self.btn.pack()

root = k()
app = Wtf(root)
app.mainloop()



Test.py:



from tkinter import *

class Test(Frame):

def __init__(self, master=None):
Frame.__init__(self, master)
self.master = master
self.pack()

def win(self):
self.label = Label(self, text="Hello World!")
self.label.pack()
self.quit = Button(self, text= "quit", command=self.master.destroy)
self.quit.pack()



Update



Based on your comment about what you really want to do, I've adapted the accepted answer to the question Switch between two frames in tkinter to your needs:



main.py



import tkinter as tk
import Test


class App(tk.Tk):

def __init__(self, *args, **kwargs):
super().__init__(*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 (Wtf, Test.Test):
page_name = F.__name__
frame = F(parent=container, controller=self)
self.frames[page_name] = frame
frame.grid(row=0, column=0, sticky="nsew")

self.show_frame("Wtf")

def show_frame(self, page_name):
""" Show a frame for the given page name. """
frame = self.frames[page_name]
frame.tkraise()


class Wtf(tk.Frame):

def __init__(self, parent, controller):
super().__init__(parent)
self.parent = parent
self.controller = controller
self.create_widgets()

def create_widgets(self):
self.test = Test.Test(self.parent, self.controller)
self.btn = tk.Button(self, text="Call",
command=lambda: self.controller.show_frame("Test"))
self.btn.pack()


if __name__ == "__main__":

app = App()
app.mainloop()



Test.py:



import tkinter as tk


class Test(tk.Frame):

def __init__(self, parent, controller):
super().__init__(parent)
self.parent = parent
self.controller = controller
self.create_widgets()

def create_widgets(self):
self.label = tk.Label(self, text="Hello World!")
self.label.pack()
self.quit = tk.Button(self, text= "Quit", command=self.controller.destroy)
self.quit.pack()





share|improve this answer

























  • sir ! it is a great program. I did understand it easily. I have changed it to control various buttons and provide more than 1 GUIs. the problem i'm occurring with it is that i'm no able to reverse it. to explain it more specifically, in this case the flow is from main to test but I want to add a back button in test which can take me back from test to main without exiting the main window.

    – Andrious Prime
    Apr 15 at 8:40











  • Andrious: If you found my answer useful, please consider at least up-voting it. See What should I do when someone answers my question? Afterwards I'll probably be more inclined to answer follow-on questions you might have...

    – martineau
    Apr 15 at 14:48













0












0








0







I don't understand exactly what you're trying to accomplish, but there are numerous problems with your code, some of which I already pointed-out in a comment.



The 'module' object is not callable problem is because you have a module called Test which defines a class named Test inside it, so the problem can be avoided by using Test.Test when referring to the class.



Here are working versions of the main script and the Test.py module:



main.py:



from tkinter import *
import Test


class Wtf(Frame):
def __init__(self, master=None):
Frame.__init__(self, master)
self.master = master
self.pack()
self.create_widgets()

def create_widgets(self):
self.test = Test.Test(self.master)
self.btn = Button(self, text="Call", command=self.test.win)
self.btn.pack()

root = k()
app = Wtf(root)
app.mainloop()



Test.py:



from tkinter import *

class Test(Frame):

def __init__(self, master=None):
Frame.__init__(self, master)
self.master = master
self.pack()

def win(self):
self.label = Label(self, text="Hello World!")
self.label.pack()
self.quit = Button(self, text= "quit", command=self.master.destroy)
self.quit.pack()



Update



Based on your comment about what you really want to do, I've adapted the accepted answer to the question Switch between two frames in tkinter to your needs:



main.py



import tkinter as tk
import Test


class App(tk.Tk):

def __init__(self, *args, **kwargs):
super().__init__(*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 (Wtf, Test.Test):
page_name = F.__name__
frame = F(parent=container, controller=self)
self.frames[page_name] = frame
frame.grid(row=0, column=0, sticky="nsew")

self.show_frame("Wtf")

def show_frame(self, page_name):
""" Show a frame for the given page name. """
frame = self.frames[page_name]
frame.tkraise()


class Wtf(tk.Frame):

def __init__(self, parent, controller):
super().__init__(parent)
self.parent = parent
self.controller = controller
self.create_widgets()

def create_widgets(self):
self.test = Test.Test(self.parent, self.controller)
self.btn = tk.Button(self, text="Call",
command=lambda: self.controller.show_frame("Test"))
self.btn.pack()


if __name__ == "__main__":

app = App()
app.mainloop()



Test.py:



import tkinter as tk


class Test(tk.Frame):

def __init__(self, parent, controller):
super().__init__(parent)
self.parent = parent
self.controller = controller
self.create_widgets()

def create_widgets(self):
self.label = tk.Label(self, text="Hello World!")
self.label.pack()
self.quit = tk.Button(self, text= "Quit", command=self.controller.destroy)
self.quit.pack()





share|improve this answer















I don't understand exactly what you're trying to accomplish, but there are numerous problems with your code, some of which I already pointed-out in a comment.



The 'module' object is not callable problem is because you have a module called Test which defines a class named Test inside it, so the problem can be avoided by using Test.Test when referring to the class.



Here are working versions of the main script and the Test.py module:



main.py:



from tkinter import *
import Test


class Wtf(Frame):
def __init__(self, master=None):
Frame.__init__(self, master)
self.master = master
self.pack()
self.create_widgets()

def create_widgets(self):
self.test = Test.Test(self.master)
self.btn = Button(self, text="Call", command=self.test.win)
self.btn.pack()

root = k()
app = Wtf(root)
app.mainloop()



Test.py:



from tkinter import *

class Test(Frame):

def __init__(self, master=None):
Frame.__init__(self, master)
self.master = master
self.pack()

def win(self):
self.label = Label(self, text="Hello World!")
self.label.pack()
self.quit = Button(self, text= "quit", command=self.master.destroy)
self.quit.pack()



Update



Based on your comment about what you really want to do, I've adapted the accepted answer to the question Switch between two frames in tkinter to your needs:



main.py



import tkinter as tk
import Test


class App(tk.Tk):

def __init__(self, *args, **kwargs):
super().__init__(*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 (Wtf, Test.Test):
page_name = F.__name__
frame = F(parent=container, controller=self)
self.frames[page_name] = frame
frame.grid(row=0, column=0, sticky="nsew")

self.show_frame("Wtf")

def show_frame(self, page_name):
""" Show a frame for the given page name. """
frame = self.frames[page_name]
frame.tkraise()


class Wtf(tk.Frame):

def __init__(self, parent, controller):
super().__init__(parent)
self.parent = parent
self.controller = controller
self.create_widgets()

def create_widgets(self):
self.test = Test.Test(self.parent, self.controller)
self.btn = tk.Button(self, text="Call",
command=lambda: self.controller.show_frame("Test"))
self.btn.pack()


if __name__ == "__main__":

app = App()
app.mainloop()



Test.py:



import tkinter as tk


class Test(tk.Frame):

def __init__(self, parent, controller):
super().__init__(parent)
self.parent = parent
self.controller = controller
self.create_widgets()

def create_widgets(self):
self.label = tk.Label(self, text="Hello World!")
self.label.pack()
self.quit = tk.Button(self, text= "Quit", command=self.controller.destroy)
self.quit.pack()






share|improve this answer














share|improve this answer



share|improve this answer








edited Mar 24 at 14:02

























answered Mar 24 at 9:43









martineaumartineau

72k1093191




72k1093191












  • sir ! it is a great program. I did understand it easily. I have changed it to control various buttons and provide more than 1 GUIs. the problem i'm occurring with it is that i'm no able to reverse it. to explain it more specifically, in this case the flow is from main to test but I want to add a back button in test which can take me back from test to main without exiting the main window.

    – Andrious Prime
    Apr 15 at 8:40











  • Andrious: If you found my answer useful, please consider at least up-voting it. See What should I do when someone answers my question? Afterwards I'll probably be more inclined to answer follow-on questions you might have...

    – martineau
    Apr 15 at 14:48

















  • sir ! it is a great program. I did understand it easily. I have changed it to control various buttons and provide more than 1 GUIs. the problem i'm occurring with it is that i'm no able to reverse it. to explain it more specifically, in this case the flow is from main to test but I want to add a back button in test which can take me back from test to main without exiting the main window.

    – Andrious Prime
    Apr 15 at 8:40











  • Andrious: If you found my answer useful, please consider at least up-voting it. See What should I do when someone answers my question? Afterwards I'll probably be more inclined to answer follow-on questions you might have...

    – martineau
    Apr 15 at 14:48
















sir ! it is a great program. I did understand it easily. I have changed it to control various buttons and provide more than 1 GUIs. the problem i'm occurring with it is that i'm no able to reverse it. to explain it more specifically, in this case the flow is from main to test but I want to add a back button in test which can take me back from test to main without exiting the main window.

– Andrious Prime
Apr 15 at 8:40





sir ! it is a great program. I did understand it easily. I have changed it to control various buttons and provide more than 1 GUIs. the problem i'm occurring with it is that i'm no able to reverse it. to explain it more specifically, in this case the flow is from main to test but I want to add a back button in test which can take me back from test to main without exiting the main window.

– Andrious Prime
Apr 15 at 8:40













Andrious: If you found my answer useful, please consider at least up-voting it. See What should I do when someone answers my question? Afterwards I'll probably be more inclined to answer follow-on questions you might have...

– martineau
Apr 15 at 14:48





Andrious: If you found my answer useful, please consider at least up-voting it. See What should I do when someone answers my question? Afterwards I'll probably be more inclined to answer follow-on questions you might have...

– martineau
Apr 15 at 14:48











-1














I am not sure to have understood well the question, anyway look below.



Particularly to




self.close_me = tk.Button(self, text= "Close me", command = self.on_close_me).pack()



self.close_parent = tk.Button(self, text= "Close parent", command = self.on_close_parent).pack()




You can even put the class Test on another file and input it.



import tkinter as tk

class Test(tk.Toplevel):

def __init__(self, parent):
super().__init__()

self.parent = parent
self.title("I'm a new toplevel.")
self.init_ui()

def init_ui(self):

self.label = tk.Label(self, text=self.title()).pack()
self.close_me = tk.Button(self, text= "Close me", command = self.on_close_me).pack()
self.close_parent = tk.Button(self, text= "Close parent", command = self.on_close_parent).pack()

def on_close_me(self):
self.destroy()

def on_close_parent(self):
self.parent.on_close()


class App(tk.Frame):

def __init__(self,):

super().__init__()

self.master.title("Hello World")

self.init_ui()

def init_ui(self):

self.pack(fill=tk.BOTH, expand=1,)

f = tk.Frame()

w = tk.Frame()

tk.Button(w, text="Open", command=self.callback).pack()
tk.Button(w, text="Close", command=self.on_close).pack()

f.pack(side=tk.LEFT, fill=tk.BOTH, expand=0)
w.pack(side=tk.RIGHT, fill=tk.BOTH, expand=0)


def callback(self):
obj = Test(self)

def on_close(self,evt=None):
self.master.destroy()

if __name__ == '__main__':
app = App()
app.mainloop()





share|improve this answer























  • .pack() returns None.

    – martineau
    Mar 24 at 9:00















-1














I am not sure to have understood well the question, anyway look below.



Particularly to




self.close_me = tk.Button(self, text= "Close me", command = self.on_close_me).pack()



self.close_parent = tk.Button(self, text= "Close parent", command = self.on_close_parent).pack()




You can even put the class Test on another file and input it.



import tkinter as tk

class Test(tk.Toplevel):

def __init__(self, parent):
super().__init__()

self.parent = parent
self.title("I'm a new toplevel.")
self.init_ui()

def init_ui(self):

self.label = tk.Label(self, text=self.title()).pack()
self.close_me = tk.Button(self, text= "Close me", command = self.on_close_me).pack()
self.close_parent = tk.Button(self, text= "Close parent", command = self.on_close_parent).pack()

def on_close_me(self):
self.destroy()

def on_close_parent(self):
self.parent.on_close()


class App(tk.Frame):

def __init__(self,):

super().__init__()

self.master.title("Hello World")

self.init_ui()

def init_ui(self):

self.pack(fill=tk.BOTH, expand=1,)

f = tk.Frame()

w = tk.Frame()

tk.Button(w, text="Open", command=self.callback).pack()
tk.Button(w, text="Close", command=self.on_close).pack()

f.pack(side=tk.LEFT, fill=tk.BOTH, expand=0)
w.pack(side=tk.RIGHT, fill=tk.BOTH, expand=0)


def callback(self):
obj = Test(self)

def on_close(self,evt=None):
self.master.destroy()

if __name__ == '__main__':
app = App()
app.mainloop()





share|improve this answer























  • .pack() returns None.

    – martineau
    Mar 24 at 9:00













-1












-1








-1







I am not sure to have understood well the question, anyway look below.



Particularly to




self.close_me = tk.Button(self, text= "Close me", command = self.on_close_me).pack()



self.close_parent = tk.Button(self, text= "Close parent", command = self.on_close_parent).pack()




You can even put the class Test on another file and input it.



import tkinter as tk

class Test(tk.Toplevel):

def __init__(self, parent):
super().__init__()

self.parent = parent
self.title("I'm a new toplevel.")
self.init_ui()

def init_ui(self):

self.label = tk.Label(self, text=self.title()).pack()
self.close_me = tk.Button(self, text= "Close me", command = self.on_close_me).pack()
self.close_parent = tk.Button(self, text= "Close parent", command = self.on_close_parent).pack()

def on_close_me(self):
self.destroy()

def on_close_parent(self):
self.parent.on_close()


class App(tk.Frame):

def __init__(self,):

super().__init__()

self.master.title("Hello World")

self.init_ui()

def init_ui(self):

self.pack(fill=tk.BOTH, expand=1,)

f = tk.Frame()

w = tk.Frame()

tk.Button(w, text="Open", command=self.callback).pack()
tk.Button(w, text="Close", command=self.on_close).pack()

f.pack(side=tk.LEFT, fill=tk.BOTH, expand=0)
w.pack(side=tk.RIGHT, fill=tk.BOTH, expand=0)


def callback(self):
obj = Test(self)

def on_close(self,evt=None):
self.master.destroy()

if __name__ == '__main__':
app = App()
app.mainloop()





share|improve this answer













I am not sure to have understood well the question, anyway look below.



Particularly to




self.close_me = tk.Button(self, text= "Close me", command = self.on_close_me).pack()



self.close_parent = tk.Button(self, text= "Close parent", command = self.on_close_parent).pack()




You can even put the class Test on another file and input it.



import tkinter as tk

class Test(tk.Toplevel):

def __init__(self, parent):
super().__init__()

self.parent = parent
self.title("I'm a new toplevel.")
self.init_ui()

def init_ui(self):

self.label = tk.Label(self, text=self.title()).pack()
self.close_me = tk.Button(self, text= "Close me", command = self.on_close_me).pack()
self.close_parent = tk.Button(self, text= "Close parent", command = self.on_close_parent).pack()

def on_close_me(self):
self.destroy()

def on_close_parent(self):
self.parent.on_close()


class App(tk.Frame):

def __init__(self,):

super().__init__()

self.master.title("Hello World")

self.init_ui()

def init_ui(self):

self.pack(fill=tk.BOTH, expand=1,)

f = tk.Frame()

w = tk.Frame()

tk.Button(w, text="Open", command=self.callback).pack()
tk.Button(w, text="Close", command=self.on_close).pack()

f.pack(side=tk.LEFT, fill=tk.BOTH, expand=0)
w.pack(side=tk.RIGHT, fill=tk.BOTH, expand=0)


def callback(self):
obj = Test(self)

def on_close(self,evt=None):
self.master.destroy()

if __name__ == '__main__':
app = App()
app.mainloop()






share|improve this answer












share|improve this answer



share|improve this answer










answered Mar 24 at 8:58









1966bc1966bc

2226




2226












  • .pack() returns None.

    – martineau
    Mar 24 at 9:00

















  • .pack() returns None.

    – martineau
    Mar 24 at 9:00
















.pack() returns None.

– martineau
Mar 24 at 9:00





.pack() returns None.

– martineau
Mar 24 at 9:00

















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55321997%2fhow-to-call-a-tkinter-class-as-an-object-or-in-button-click%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript