Home > Back-end >  Problem with the location of TKinter widgets
Problem with the location of TKinter widgets

Time:02-02

I started working on the code, and when I put the widgets in places, there was a problem, the widgets move down I.e. on pressing the button that adds more "entry" fields they appear very crooked, and I do not know why, what to do with this, and what I did wrong? 1: https://i.stack.imgur.com/b8DBm.png - a picture of my problem

import tkinter as tk
from tkinter import ttk
import requests
import time
from tkinter import *
from tkinter import messagebox




window = Tk()
window.geometry('400x700')
window.title("SiteChecker")

def clicked():
    txt = Entry(window, width=18)
    txt.grid(column=0, pady=8)

    tim = Entry(window, width=3)
    tim.grid(column=1, pady=8)





lbl1 = Label(window, text="Enter links: ")
lbl1.grid(column=0, row=1)
lbl2 = Label(window, text="Enter the test time: ")
lbl2.grid(column=1, row=1)
lbl3 = Label(window, text="Availability status ")
lbl3.grid(column=2, row=1)


txt1 = Entry(window,width=18)
txt1.grid(column=0, row=2, pady=8)
txt2 = Entry(window,width=18)
txt2.grid(column=0, row=3,pady=8)
txt3 = Entry(window,width=18)
txt3.grid(column=0, row=4,pady=8)
txt4 = Entry(window,width=18)
txt4.grid(column=0, row=5,pady=8)
txt5 = Entry(window,width=18)
txt5.grid(column=0, row=6,pady=8)

tim1 = Entry(window,width=3)
tim1.grid(column=1, row=2, pady=8)
tim2 = Entry(window,width=3)
tim2.grid(column=1, row=3, pady=8)
tim3 = Entry(window,width=3)
tim3.grid(column=1, row=4, pady=8)
tim4 = Entry(window,width=3)
tim4.grid(column=1, row=5, pady=8)
tim5 = Entry(window,width=3)
tim5.grid(column=1, row=6, pady=8)


btn = Button(window, text="Add more site", command=clicked)
btn.grid(column=1, row = 0)


window.mainloop()

CodePudding user response:

The new line isn't "crooked." The second Entry widget is being placed on the next row, because the row value auto-increments in calls to .grid() if not declared.

You could just keep track of on which row you want to place the new pair of widgets. Or, you can place the first one, which works as expected. Then ask the layout manager on which row it placed the first one, and then place the second widget on that same row.

Here's the code that does that:

def clicked():
    txt = Entry(window, width=18)
    txt.grid(column=0, pady=8)
    txt_row = txt.grid_info()['row'] # the row the widget was mapped onto

    tim = Entry(window, width=3)
    tim.grid(row=txt_row, column=1, pady=8) # put the 2nd widget on the same row
  • Related