0

I have some code that generates a passphrase of varying length displaying the passphrase over a button to generate it. The problem is that even though both widgets are spanning 2 columns and I've called pack_propogate(0) the widgets shift left and right ever so slightly as I generate passphrases with different lengths.

For full context, this is my code so far:

from tkinter import *
from tkinter import ttk, Tk
import ctypes
from diceware import get_passphrase

user32 = ctypes.windll.user32
user32.SetProcessDPIAware()

root = Tk()
root.title('Diceware')

mainframe = ttk.Frame(root)
mainframe.grid(column=0, row=0)
mainframe.pack_propagate(0)

root.columnconfigure(0, weight=1)
root.rowconfigure(0, weight=1)

passphrase = StringVar()
passphrase.set('passphrase')


def generate(*args):
    passphrase.set(get_passphrase())


output_label = ttk.Label(mainframe, textvariable=passphrase, justify='center').grid(column=0, columnspan=2, row=0)
generate_button = ttk.Button(mainframe, text='Generate', command=generate).grid(column=0, columnspan=2, row=1)

pad_x = 5
pad_y = 5
for child in mainframe.winfo_children():
    child.grid_configure(padx=pad_x, pady=pad_y)

root.geometry('400x100')
root.resizable(0,0)

root.bind("<Return>", generate)

root.mainloop()
  • 1
    Since the question is about widget layout, do you really need ctypes and diceware for the purposes of this question? It would be better if you could hard-code some sample data. – Bryan Oakley Jun 05 '20 at 19:07
  • Read [AttributeError: NoneType object has no attribute](https://stackoverflow.com/a/1101765/7414759) – stovfl Jun 05 '20 at 19:25
  • How about if you replace ```mainframe.pack_propagate(0)``` with ```mainframe.grid_propagate(0)```. I'm not ertain if the function exists, but you either use pack or grid. using both on the same object shouldn't work. – PythonAmateur742 Jun 05 '20 at 19:32
  • Layout it into a `Frame` to make both widgets independant. See [Live-Demo](https://repl.it/repls/ExcitingSkyblueFlashdrives#main.py). But how will you handle it, if the passphrase is longer then the window width? – stovfl Jun 05 '20 at 19:37
  • stovfl I checked that out and reformatted my code appropriately. Not a solution, but thank you for the tip. – Everett Jun 05 '20 at 21:57
  • PythonAmateur742 that is a function but it unfortunateley did not solve my problem. – Everett Jun 05 '20 at 22:00

0 Answers0