-1

I am using the Button, Entry, Label element, as well as using winfo_screenwidth(), winfo_screenwidth(), and the geometry() function for my main window. On the Entry elements I am only using the get() and delete() functions.

from Tkinter import *
def main():
    window = Tk()
    window.title('cool stuff')

    width = 750
    height = 60

    screenw = window.winfo_screenwidth()
    screenh = window.winfo_screenheight()

    posx = (screenw/2) - (width/2)
    posy = (screenh/2) - (height/2)

    window.geometry('%dx%d+%d+%d' % (width, height, posx, posy))

    Label(window, text='enter text: ').pack(side=LEFT)
    textEntry = Entry(window)
    textEntry.config(width=16)
    textEntry.pack(side=LEFT)

    submitButton = Button(window, text='Submit', width=12, command=lambda: buttonCallback(textEntry))
    submitButton.pack()
    window.mainloop()

# in buttonCallback Entry.get() and Entry.delete() functions are used...

Is there a way to make my imports weigh less in this scenario? I am trying to compile the python file to .exe, but it requires a lot of files by its side to function. It would also make sense to shrink down imports in a lot of other cases, because I guess you'd want less overhead for your python files to run faster.

spelgubbe
  • 56
  • 4
  • 4
    you could try `from Tkinter import what_you_need`? – evan54 May 27 '16 at 00:01
  • 1
    Closely related question though not an exact duplicate: http://stackoverflow.com/questions/5877218/python-imports – paisanco May 27 '16 at 00:04
  • 1
    FWIW, doing `from Tkinter import *` clutters your namespace with 175 names. – PM 2Ring May 27 '16 at 00:26
  • if you want small .exe, python is probably not a great choice ... I have yet to have less than a 10 MB exe from python programs ... (maybe a slight exaggeration) ... to be fair GUI's take a lot of resources, and im not sure it would be alot smaller in any other language. .. – Joran Beasley May 27 '16 at 00:31
  • ... you are only importing `Tkinter`... it is only one file... do you mean you want to package your python app without including the tk resources? – Tadhg McDonald-Jensen May 27 '16 at 00:31
  • At the end of the day, the .exe will need to contain a Python installation and then the GUI library will probably double the size. From my experience, both are about 6 MB, which isn't unreasonable. I don't think specific imports will reduce the file size since the entire script will need to be packaged (you have no way to know there isn't a module-scoped variable that needs to be initialized). IMO, it's probably not worth it to look into this too much. – Jared Goguen May 27 '16 at 00:39
  • the problem is, I am not sure what_I_need, for example: I can not seem to find a way to use Tk() without importing the whole library, and I can not find a way to import functions like winfo_screenwidth(). Writing `from Tkinter import Tk` won't work, `from Tkinter import geometry` won't work, `from Tkinter import Button, Label, Entry` works though However, if I need to use Tk() I can not find a way to do so without importing the whole Tkinter library :/ – spelgubbe May 27 '16 at 01:33
  • 1
    Sorry if my earlier comment was misleading. It was **only** an incidental remark to mention the clutter that star imports create, and the potential for name clashes. When you do _any_ import the whole module _has_ to be loaded. Eg, if you do `from Tkinter import Button` the whole Tkinter module is loaded, but only the `Button` name appears in the current namespace. If the whole module wasn't loaded then `Button` wouldn't have access to the Tkinter stuff it needs to do its job. See http://programmers.stackexchange.com/a/187471 – PM 2Ring May 27 '16 at 13:57
  • Thank you for pointing that out! I didn't know it worked that way :P – spelgubbe May 28 '16 at 17:56
  • @TadhgMcDonald-Jensen: tkinter is much more than a single file. – Bryan Oakley May 29 '16 at 15:35

1 Answers1

0

There is nothing you can do to make the tkinter imports 'weigh less", if by that you mean a smaller memory footprint.

You must import all of Tkinter in order for it to be usable; you can't pick and choose a subset of widgets from Tkinter. The bulk of the tkinter code isn't even python; tkinter is a thin wrapper around a complete tcl interpreter and tk GUI library, which must be imported as an atomic unit.

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685