title: With Tkinter, how to position a dialog box at initiation OR how to auto-resize a window after geometry is set?
body: <p><strong>Disclaimer:</strong> In all likelihood this could very well be an XY problem, I'd appreciate if you would point me to the correct direction.</p>
  
  <p><strong>Goal:</strong> I have a file that I'd like tkinter to read and then create widgets dynamically based on the file contents.  <strong>I want the window to always open centered at my cursor.</strong></p>
  
  <p>For the sake of MCVE let's consider a plain text file that reads:</p>
  
  <pre class="lang-none prettyprint-override"><code>Madness?
  This
  IS
  T K I N T E R
  </code></pre>
  
  <p>And tkinter should create 4 label widgets, with the main window resized to fit.  That's very simple... <em>until the file is encrypted.</em>  Before I can read the encrypted file, I need to <code>askstring()</code> for a password first (MCVE: let's also assume we're only asking for the <code>key</code> here).</p>
  
  <p><strong>My issues:</strong></p>
  
  <ol>
  <li><p>The <code>askstring</code> Dialog is always at a default position.  I know it's supposed to be relative to the parent (root)...</p></li>
  <li><p>But I can't set <code>geometry()</code> before I create the widgets or else it won't resize to the widgets...</p></li>
  <li><p>And I can't pre-determine the size required for <code>geometry()</code> since I can't open the file without the password (key)...</p></li>
  </ol>
  
  <p>Here's a MCVE sample of my most successful attempt:</p>
  
  <pre><code>import tkinter as tk
  from tkinter.simpledialog import askstring
  from cryptography.fernet import Fernet
  
  class GUI(tk.Tk):
      def __init__(self):
          super().__init__()
  
          # side note: If I don't withdraw() the root first, the dialog ends up behind the root
          # and I couldn't find a way to get around this.
          self.withdraw()
          self.create_widgets()
  
          # Attempt: I tried doing this instead of self.create_widgets():
              # self.reposition()
              # self.after(ms=1,func=self.create_widgets)
          # The dialog is positioned correctly, but the window size doesn't resize
  
          self.deiconify()
  
          # I have to do the reposition AFTER mainloop or else the window size becomes 1x1
          self.after(ms=1, func=self.reposition)
          self.mainloop()
  
      def get_key(self):
          return askstring('Locked','Enter Key', show='*', parent=self)
  
      def create_widgets(self):
          self.lbls = []
          with open('test2.txt', 'rb') as file:
              encrypted = file.read()
          key = self.get_key()
          suite = Fernet(key)
          self.data = suite.decrypt(encrypted).decode('utf-8')
          for i in self.data.split('\n'):
              self.lbls.append(tk.Label(self, text=i.strip()))
              self.lbls[-1].pack()
  
      def reposition(self):
          width, height = self.winfo_width(), self.winfo_height()
          self.geometry(f'{width}x{height}+{self.winfo_pointerx()-int(width/2)}+{self.winfo_pointery()-int(height/2)}')
  
  gui = GUI()
  </code></pre>
  
  <p>Which achieves (that I can live with):</p>
  
  <ul>
  <li>✓ Correct size based on file content</li>
  <li>✓ Root positioned at center of cursor</li>
  <li>⨯ Prompts for key at center of cursor</li>
  </ul>
  
  <p><strong>My questions are:</strong></p>
  
  <ol>
  <li><p>Is it possible to perform an auto-resize on the root again based on the widgets function similar to <code>pack_propagate()</code> after <code>geometry()</code> is set? It seems once <code>geometry()</code> is set the root won't propagate no more.</p></li>
  <li><p>If not, how can I manually resize it in code? I tried retrieving the total heights of the widgets <code>height = sum([i.winfo_reqheight() for i in self.lbls])</code> but <code>height</code> just becomes <code>0</code>.  But when I <code>print(self.lbls[-1].winfo_reqheight())</code> in the <code>self.create_widgets()</code> it returns <code>26</code> each, and they actually print <em>after</em> my <code>self.reposition()</code> call, which is weird.</p></li>
  <li><p>Failing that, is it possible to position the <code>askstring()</code> dialog prior to the the widgets being created?</p></li>
  </ol>
  
  <p>I'm stumped.  I feel like I'm going about this the wrong way but I'm not sure what is the correct way to handle this situation to break the cycle of dependency.</p>
  
  <hr>
  
  <p>To help with reproducing here's the encrypted string as well as the key:</p>
  
  <p>Data:</p>
  
  <pre class="lang-none prettyprint-override"><code>gAAAAABb2z3y-Kva7bdgMEbvnqGGRRJc9ZMrt8oc092_fuxSK1x4qlP72aVy13xR-jQV1vLD7NQdOTT6YBI17pGYpUZiFpIOQGih9jYsd-u1EPUeV2iqPLG7wYcNxYq-u4Z4phkHllvP
  </code></pre>
  
  <p>Key:</p>
  
  <pre class="lang-none prettyprint-override"><code>SecretKeyAnyhowGoWatchDareDevilS3ItsAmazing=
  </code></pre>
  
  <p>Edit: Based on <a href="https://stackoverflow.com/questions/23853575/tkinter-geometry-return-to-normal">@BryanOakley's answer here</a> I can invoke <code>self.geometry("")</code> to reset, however it goes back to the native <code>200x200</code> size, and still doesn't propragate the widgets.</p>