1

I created a package. It's a function I need to use in almost every Tkinter project of mine. It centers my Tkinter window on the screen of the user. How do I make it like a global package like "import pandas" or "import math"?

This is the code for my package:

MyPkg.py

def Centre(NameOfTkinterWindow,Width,Height):#This function Centres the Tkinter window on the screen
    scrwdth = NameOfTkinterWindow.winfo_screenwidth()
    scrhgt = NameOfTkinterWindow.winfo_screenheight()
    xLeft = int((scrwdth/2) - (Width/2))
    yTop = int((scrhgt/2) - (Height/2))
    NameOfTkinterWindow.geometry(str(Width) + "x" + str(Height) + "+" + str(xLeft) + "+" + str(yTop))

This is my Tkinter project that I am working on: main.py

from tkinter import *
from MyPkg import * #importing my custom built package
main=Tk()
main.title("Test Taking App")
Centre(main,500,500) #A function from my Package
main.iconbitmap("D:\Coding\Python Scripts\PDF Convertor App\DevenIcon.ico")
main.mainloop()

Everything is working as intended:

Image of my Program in Action

But I want to make it so that I don't need to have this MyPkg.py in the same folder as my Tkinter projects as I make many different softwares and each one has their own folder.

enter image description here

I want to be able to import it anywhere on my computer from any directory. What can I try next?

halfer
  • 19,824
  • 17
  • 99
  • 186
Deven Jain
  • 27
  • 1
  • 8

2 Answers2

1

On a unix machine adding your module to /usr/lib/python3.9/ or one of the other paths where python searches for modules would do the trick. Hopefully doing the equivalent of that on windows should work (The path will of course not be the same tho).

1

Go to

My Computer > Properties > Advanced System Settings > Environment Variables >

Then under system variables create a new Variable called PYTHONPATH. In this variable add the location of your modules.

PYTHONPATH

kinshukdua
  • 1,944
  • 1
  • 5
  • 15