Two classes having references to each other
I'm working on an asynchronous TCP server.
All is going well, and I'm learning a lot from it, but I have a question related to the structure.
This is how the structure looks:
The Server object is an asynchronous TCP server running in its own thread.
It communicates with the GUI Controller object via thread-safe queue's.
On the main thread we've got the GUI Controller , the tkinter mainloop and the actual Tkinter GUI class.
here is some code to make it more clear:
clientlist =
#queue's for communicating with server thread
buffer = queue.Queue()
buffer2 = queue.Queue()
root = Tk()
#GUI controller
controller = Controller(buffer,buffer2,clientlist,root)
#Make Tkinter GUI and give a reference to the controller
top = MainWindow(controller, root)
#Giving Tkinter GUI reference to the controller
controller.set_top(top)
t1 = threading.Thread(target=run_server, args=(buffer,buffer2))
t1.start()
controller.check_connections()
root.protocol("WM_DELETE_WINDOW", on_closing)
try:
while controller.run:
root.mainloop()
print("[i] Joining server thread.")
t1.join()
I have a hard time finding a nice way to make the Tkinter GUI communicate with the GUI controller. The idea I had here to give a reference to each others class is not working:
If I give the MainWindow a reference to Controller, I need to save it to a variable in the constructor of MainWindow. Then I need to update that variable each loop which is very demanding.
How can I give a reference the correct way, or is there a better way to let these two classes communicate?
Thanks a lot!
python tkinter server reference structure
add a comment |
I'm working on an asynchronous TCP server.
All is going well, and I'm learning a lot from it, but I have a question related to the structure.
This is how the structure looks:
The Server object is an asynchronous TCP server running in its own thread.
It communicates with the GUI Controller object via thread-safe queue's.
On the main thread we've got the GUI Controller , the tkinter mainloop and the actual Tkinter GUI class.
here is some code to make it more clear:
clientlist =
#queue's for communicating with server thread
buffer = queue.Queue()
buffer2 = queue.Queue()
root = Tk()
#GUI controller
controller = Controller(buffer,buffer2,clientlist,root)
#Make Tkinter GUI and give a reference to the controller
top = MainWindow(controller, root)
#Giving Tkinter GUI reference to the controller
controller.set_top(top)
t1 = threading.Thread(target=run_server, args=(buffer,buffer2))
t1.start()
controller.check_connections()
root.protocol("WM_DELETE_WINDOW", on_closing)
try:
while controller.run:
root.mainloop()
print("[i] Joining server thread.")
t1.join()
I have a hard time finding a nice way to make the Tkinter GUI communicate with the GUI controller. The idea I had here to give a reference to each others class is not working:
If I give the MainWindow a reference to Controller, I need to save it to a variable in the constructor of MainWindow. Then I need to update that variable each loop which is very demanding.
How can I give a reference the correct way, or is there a better way to let these two classes communicate?
Thanks a lot!
python tkinter server reference structure
2
Set a placeholder for Window object inController.__init__
and create a register function which can reassign it when called.
– Dashadower
Jan 19 at 14:04
Thanks for your reply! The Window object needs to know the Controller object as well. So I could apply the same technique there. But then I would still need to call the register function each time the object or its properties change right? Or is there a way to give it an actual reference that points to the object?
– Jurze
Jan 19 at 15:20
1
Relevant: Mutli-threading with Tkinter
– stovfl
Jan 19 at 18:47
@stovfl I already use the queue's for communicating between multiple threads.The question was about two objects in the same thread. I did manage to get it working though, I found that I forgot a fewself.
's. So I was giving a reference the correct way. But I was sometimes writing to the object in the global scope, instead of the actual reference (both classes were in the same .py file) Thanks for your help both!
– Jurze
Jan 20 at 12:54
add a comment |
I'm working on an asynchronous TCP server.
All is going well, and I'm learning a lot from it, but I have a question related to the structure.
This is how the structure looks:
The Server object is an asynchronous TCP server running in its own thread.
It communicates with the GUI Controller object via thread-safe queue's.
On the main thread we've got the GUI Controller , the tkinter mainloop and the actual Tkinter GUI class.
here is some code to make it more clear:
clientlist =
#queue's for communicating with server thread
buffer = queue.Queue()
buffer2 = queue.Queue()
root = Tk()
#GUI controller
controller = Controller(buffer,buffer2,clientlist,root)
#Make Tkinter GUI and give a reference to the controller
top = MainWindow(controller, root)
#Giving Tkinter GUI reference to the controller
controller.set_top(top)
t1 = threading.Thread(target=run_server, args=(buffer,buffer2))
t1.start()
controller.check_connections()
root.protocol("WM_DELETE_WINDOW", on_closing)
try:
while controller.run:
root.mainloop()
print("[i] Joining server thread.")
t1.join()
I have a hard time finding a nice way to make the Tkinter GUI communicate with the GUI controller. The idea I had here to give a reference to each others class is not working:
If I give the MainWindow a reference to Controller, I need to save it to a variable in the constructor of MainWindow. Then I need to update that variable each loop which is very demanding.
How can I give a reference the correct way, or is there a better way to let these two classes communicate?
Thanks a lot!
python tkinter server reference structure
I'm working on an asynchronous TCP server.
All is going well, and I'm learning a lot from it, but I have a question related to the structure.
This is how the structure looks:
The Server object is an asynchronous TCP server running in its own thread.
It communicates with the GUI Controller object via thread-safe queue's.
On the main thread we've got the GUI Controller , the tkinter mainloop and the actual Tkinter GUI class.
here is some code to make it more clear:
clientlist =
#queue's for communicating with server thread
buffer = queue.Queue()
buffer2 = queue.Queue()
root = Tk()
#GUI controller
controller = Controller(buffer,buffer2,clientlist,root)
#Make Tkinter GUI and give a reference to the controller
top = MainWindow(controller, root)
#Giving Tkinter GUI reference to the controller
controller.set_top(top)
t1 = threading.Thread(target=run_server, args=(buffer,buffer2))
t1.start()
controller.check_connections()
root.protocol("WM_DELETE_WINDOW", on_closing)
try:
while controller.run:
root.mainloop()
print("[i] Joining server thread.")
t1.join()
I have a hard time finding a nice way to make the Tkinter GUI communicate with the GUI controller. The idea I had here to give a reference to each others class is not working:
If I give the MainWindow a reference to Controller, I need to save it to a variable in the constructor of MainWindow. Then I need to update that variable each loop which is very demanding.
How can I give a reference the correct way, or is there a better way to let these two classes communicate?
Thanks a lot!
python tkinter server reference structure
python tkinter server reference structure
edited Jan 23 at 20:23
marc_s
575k12811101257
575k12811101257
asked Jan 19 at 14:00
JurzeJurze
547
547
2
Set a placeholder for Window object inController.__init__
and create a register function which can reassign it when called.
– Dashadower
Jan 19 at 14:04
Thanks for your reply! The Window object needs to know the Controller object as well. So I could apply the same technique there. But then I would still need to call the register function each time the object or its properties change right? Or is there a way to give it an actual reference that points to the object?
– Jurze
Jan 19 at 15:20
1
Relevant: Mutli-threading with Tkinter
– stovfl
Jan 19 at 18:47
@stovfl I already use the queue's for communicating between multiple threads.The question was about two objects in the same thread. I did manage to get it working though, I found that I forgot a fewself.
's. So I was giving a reference the correct way. But I was sometimes writing to the object in the global scope, instead of the actual reference (both classes were in the same .py file) Thanks for your help both!
– Jurze
Jan 20 at 12:54
add a comment |
2
Set a placeholder for Window object inController.__init__
and create a register function which can reassign it when called.
– Dashadower
Jan 19 at 14:04
Thanks for your reply! The Window object needs to know the Controller object as well. So I could apply the same technique there. But then I would still need to call the register function each time the object or its properties change right? Or is there a way to give it an actual reference that points to the object?
– Jurze
Jan 19 at 15:20
1
Relevant: Mutli-threading with Tkinter
– stovfl
Jan 19 at 18:47
@stovfl I already use the queue's for communicating between multiple threads.The question was about two objects in the same thread. I did manage to get it working though, I found that I forgot a fewself.
's. So I was giving a reference the correct way. But I was sometimes writing to the object in the global scope, instead of the actual reference (both classes were in the same .py file) Thanks for your help both!
– Jurze
Jan 20 at 12:54
2
2
Set a placeholder for Window object in
Controller.__init__
and create a register function which can reassign it when called.– Dashadower
Jan 19 at 14:04
Set a placeholder for Window object in
Controller.__init__
and create a register function which can reassign it when called.– Dashadower
Jan 19 at 14:04
Thanks for your reply! The Window object needs to know the Controller object as well. So I could apply the same technique there. But then I would still need to call the register function each time the object or its properties change right? Or is there a way to give it an actual reference that points to the object?
– Jurze
Jan 19 at 15:20
Thanks for your reply! The Window object needs to know the Controller object as well. So I could apply the same technique there. But then I would still need to call the register function each time the object or its properties change right? Or is there a way to give it an actual reference that points to the object?
– Jurze
Jan 19 at 15:20
1
1
Relevant: Mutli-threading with Tkinter
– stovfl
Jan 19 at 18:47
Relevant: Mutli-threading with Tkinter
– stovfl
Jan 19 at 18:47
@stovfl I already use the queue's for communicating between multiple threads.The question was about two objects in the same thread. I did manage to get it working though, I found that I forgot a few
self.
's. So I was giving a reference the correct way. But I was sometimes writing to the object in the global scope, instead of the actual reference (both classes were in the same .py file) Thanks for your help both!– Jurze
Jan 20 at 12:54
@stovfl I already use the queue's for communicating between multiple threads.The question was about two objects in the same thread. I did manage to get it working though, I found that I forgot a few
self.
's. So I was giving a reference the correct way. But I was sometimes writing to the object in the global scope, instead of the actual reference (both classes were in the same .py file) Thanks for your help both!– Jurze
Jan 20 at 12:54
add a comment |
1 Answer
1
active
oldest
votes
I fixed this when moving both the Tkinter GUI class and the controller class to their own .py
file instead of having both in the same file.
I found that I forgot some self.
's so the controller would try to acces the GUI object defined in the global scope instead of the reference I gave. After placing those self.
's all is working as I expected!
So for other people:
If you want to give two objects a reference to each other:
- Set a placeholder for the other object in each constructor. (thanks @Dashadower)
Give each class a method to reassign that placeholder with the actual reference
(Give the object as an argument)Dont forget to call the objects reference of the other object. (USE
self.
)
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54267856%2ftwo-classes-having-references-to-each-other%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
I fixed this when moving both the Tkinter GUI class and the controller class to their own .py
file instead of having both in the same file.
I found that I forgot some self.
's so the controller would try to acces the GUI object defined in the global scope instead of the reference I gave. After placing those self.
's all is working as I expected!
So for other people:
If you want to give two objects a reference to each other:
- Set a placeholder for the other object in each constructor. (thanks @Dashadower)
Give each class a method to reassign that placeholder with the actual reference
(Give the object as an argument)Dont forget to call the objects reference of the other object. (USE
self.
)
add a comment |
I fixed this when moving both the Tkinter GUI class and the controller class to their own .py
file instead of having both in the same file.
I found that I forgot some self.
's so the controller would try to acces the GUI object defined in the global scope instead of the reference I gave. After placing those self.
's all is working as I expected!
So for other people:
If you want to give two objects a reference to each other:
- Set a placeholder for the other object in each constructor. (thanks @Dashadower)
Give each class a method to reassign that placeholder with the actual reference
(Give the object as an argument)Dont forget to call the objects reference of the other object. (USE
self.
)
add a comment |
I fixed this when moving both the Tkinter GUI class and the controller class to their own .py
file instead of having both in the same file.
I found that I forgot some self.
's so the controller would try to acces the GUI object defined in the global scope instead of the reference I gave. After placing those self.
's all is working as I expected!
So for other people:
If you want to give two objects a reference to each other:
- Set a placeholder for the other object in each constructor. (thanks @Dashadower)
Give each class a method to reassign that placeholder with the actual reference
(Give the object as an argument)Dont forget to call the objects reference of the other object. (USE
self.
)
I fixed this when moving both the Tkinter GUI class and the controller class to their own .py
file instead of having both in the same file.
I found that I forgot some self.
's so the controller would try to acces the GUI object defined in the global scope instead of the reference I gave. After placing those self.
's all is working as I expected!
So for other people:
If you want to give two objects a reference to each other:
- Set a placeholder for the other object in each constructor. (thanks @Dashadower)
Give each class a method to reassign that placeholder with the actual reference
(Give the object as an argument)Dont forget to call the objects reference of the other object. (USE
self.
)
answered Jan 20 at 13:07
JurzeJurze
547
547
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54267856%2ftwo-classes-having-references-to-each-other%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
2
Set a placeholder for Window object in
Controller.__init__
and create a register function which can reassign it when called.– Dashadower
Jan 19 at 14:04
Thanks for your reply! The Window object needs to know the Controller object as well. So I could apply the same technique there. But then I would still need to call the register function each time the object or its properties change right? Or is there a way to give it an actual reference that points to the object?
– Jurze
Jan 19 at 15:20
1
Relevant: Mutli-threading with Tkinter
– stovfl
Jan 19 at 18:47
@stovfl I already use the queue's for communicating between multiple threads.The question was about two objects in the same thread. I did manage to get it working though, I found that I forgot a few
self.
's. So I was giving a reference the correct way. But I was sometimes writing to the object in the global scope, instead of the actual reference (both classes were in the same .py file) Thanks for your help both!– Jurze
Jan 20 at 12:54