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?

3
  • 1
    You don't have a package. You are having two modules. Commented May 12, 2021 at 12:15
  • 2
    You can put your module files in a folder and add that folder to PYTHONPATH environment variable. Commented May 12, 2021 at 12:20
  • I am new to this so please excuse me if some terminologies are wrong. Commented May 12, 2021 at 12:59

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

4 Comments

in the "Variable Value" field?
The location of the folder where your modules (the python files) are located.
what is the meaning of "foo"?
that was just an example folder, ignore that. Instead add your own folder, I think it's something like D:\...\Python Scripts\Test Talking App fill the dots with the correct location.
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).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.