0

Let's say I have 3 files -

  1. main.py
  2. CommunicationsHandler.py
  3. main_canvas.py

Contents of main.py

# Showing only main part
from CommunicationsHandler import CommunicationsHandler

def replace_window(event):
    root.geometry('+0+0')

l.bind('<ButtonRelease-1>', replace_window)
CommunicationsHandler.root = root

import main_canvas
from main_canvas import *

# root = CommunicationsHandler.root

Contents of main_canvas.py

# Showing only main part
from CommunicationsHandler import CommunicationsHandler

# I had to keep this name to root as I created main.py later
root = tk.Toplevel(CommunicationsHandler.root)
CommunicationsHandler.child = root

Contents of CommunicationsHandler

# Showing only main part
class CommunicationsHandler:
    root = None
    child = None

Now when <ButtonRelease-1> binding is fired in main.py, it doesn't change the geometry of GUI of main.py rather it changes the GUI of main_canvas.py how is this possible? I solved the problem by uncommenting the last line of main.py but still is it automatically changing its root or what?

Mark Setchell
  • 191,897
  • 31
  • 273
  • 432
Blake
  • 28
  • 2
  • 10
  • Please provide a [mcve] which can be used to reproduce the problem. – Bryan Oakley Mar 24 '21 at 05:55
  • It is because after `from main_canvas import *` `root` inside `main.py` will be override by `root` inside `main_canvas`. You can remove the line `from main_canvas import *` and see the difference. – acw1668 Mar 24 '21 at 05:59
  • Hi @acw1668 You are AWESOME! Thanks a lot! I don't know why I didn't think of that. If you would like, please post this comment as the answer so I can put it up as accepted. – Blake Mar 24 '21 at 07:33

1 Answers1

0

It is because after executing the following line:

from main_canvas import *

the root inside main.py will be override by root inside main_canvas module.

If you remove that line from main.py, you will see the difference.

It is one of the reason why from xxx import * is not recommended.

acw1668
  • 40,144
  • 5
  • 22
  • 34