Search code examples
pythontkinterexpandtkinter-entry

how do i use expand on entrys while i can position them python 3 tkinter


I want to make entrys move while resizing. an example is this code. i didnt want to upload the 300 line code because it does not have to do anything with the design. as you can see when you resize it horizontaly the entry2 is moving horizontaly how do i do the same for entry1?

i tried the pack(expand=True) on entry1 but then the entry2 is placed in the position of button.

im sorry if i cant make the problem clear this is my first time asking questions about programming...

import tkinter as tk

app = tk.Tk()

app.geometry("600x350")

Canvas = tk.Canvas(height=600, width=1000, bg="#343434")
Canvas.pack(fill="both", expand=True)

Frame = tk.Frame(Canvas, height=600, width=1000, bg="#1A1A1A")
Frame.pack(fill="both", expand=True)

large_font = ('Verdana', 17)
entry1 = tk.Entry(Frame, bg="#F7F5EB", font=large_font, width=25)
entry1.place(rely=0.3, relx=0.17)
entry1.configure(foreground="gray")

entry2 = tk.Entry(Frame, bg="#F7F5EB", width=22, font=("Verdana", 11))
entry2.pack(expand=True)
entry2.configure(foreground="gray")

generate_button_font = ('Arial', 12)
Generate_button = tk.Button(
Frame, bg="#f0f0f0", font=generate_button_font, foreground="#525252")
Generate_button.place(rely=0.7, relx=0.35, relwidth=0.31, relheight=0.12)
Generate_button.config(text="Generate")

help_button_font = ("Arial", 10)
help_button = tk.Button(Frame, bg="#F7F5EB", font=help_button_font, foreground="#525252")
help_button.place(rely=0.03, relx=0.88, relwidth=0.1, relheight=0.075)
help_button.config(text="Help")

about_button_font = ("Arial", 10)
about_button = tk.Button(Frame, bg="#F7F5EB", font=help_button_font, foreground="#525252")
about_button.place(rely=0.03, relx=0.02, relwidth=0.1, relheight=0.075)
about_button.config(text="About")


app.mainloop()

Solution

  • You can use the .place() geometry manager.

    Read more about it here.

    Place has 3 main paramaters which I think you need.

    The first is relx. This places any widget at a certain position in a widget, relative to the widgets x legnth.

    For example, if the relx = 0.5, then no matter how much you resize the widget, the widget stays in the middle.

    The second parameter is rely, which works the same as relx, except it applies to the y axis.

    The third paramater is anchor. Anchor basically tells the program which part of the widget should go to the specified coordinate. The default is "nw" or top left. You can put it as center, or anything else.

    Hopefully this helps!!