Home > Enterprise >  Python tkinter text not align in in pack
Python tkinter text not align in in pack

Time:06-08

enter image description here

text not properly align in pack. I want the text to be on top and pack on bottom but every time I try to align the "Enter Password Length" disappear

from tkinter import Button, Entry , Label, Tk
from tkinter.constants import END
import pyperclip
import string




password_chars = string.ascii_letters   string.digits   string.punctuation

class gui:
    def __init__(self):
        self.window = Tk()
        self.window.title("Password Generator")
        self.window.geometry("300x300")
        # Label Frame
        label_title = Label(text="Password Generator", fg="#800000",font=("Inter", 15, "bold"))
        label_title.pack(pady=15)
        self.label = Label(
        self.window, text="Enter Password Length",font=("Inter"))
        self.label.pack(pady=20,)
        # Entry box for number of characters
        self.length_entry_box = Entry(self.label, width=24,)
        self.length_entry_box.pack(padx=10, pady=15)

if __name__ == '__main__':
    gui().window.mainloop()

CodePudding user response:

The problem is you give the master option in the entry as self.label this should be self.window.

master=self.label(or the first parameter) means entry should inside self.label.


from tkinter import Button, Entry, Label, Tk
from tkinter.constants import END
import pyperclip
import string




password_chars = string.ascii_letters   string.digits   string.punctuation

class gui:
    def __init__(self):
        self.window = Tk()
        self.window.title("Password Generator")
        self.window.geometry("300x300")
        # Label Frame
        label_title = Label(text="Password Generator", fg="#800000",font=("Inter", 15, "bold"))
        label_title.pack(pady=15)
        self.label = Label(
        self.window, text="Enter Password Length",font=("Inter"))
        self.label.pack(pady=20,)
        # Entry box for number of characters
        self.length_entry_box = Entry(self.window, width=24,) # self.label changes to self.window.
        self.length_entry_box.pack(padx=10, pady=15)

if __name__ == '__main__':
    gui().window.mainloop()

CodePudding user response:

In line 23. Removed object self.label,

from tkinter import Button, Entry , Label, Tk
from tkinter.constants import END
import pyperclip
import string


password_chars = string.ascii_letters   string.digits   string.punctuation

class gui:
    def __init__(self):
        self.window = Tk()
        self.window.title("Password Generator")
        self.window.geometry("300x300")
        # Label Frame
        label_title = Label(text="Password Generator", fg="#800000",font=("Inter", 15, "bold"))
        label_title.pack(pady=15)
        self.label = Label(
        self.window, text="Enter Password Length",font=("Inter"))
        self.label.pack(pady=20,)
        # Entry box for number of characters
        self.length_entry_box = Entry(width=24)
        self.length_entry_box.pack(padx=10, pady=15)

if __name__ == '__main__':
    gui().window.mainloop()

Output:

enter image description here

  • Related