Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to place an entry field next to label correctly using Python Tkinter?

I have got a problem with spacing entry fields in window. It places not next to the label.

I tried to change numbers of rows and colomns to place it right next to labels, bit it doesn’t work. Now these fields just levitate.
Window
Here is my code.

from tkinter import *
from tkinter import ttk
import tkinter as tk
from tkinter import Tk


class Window(Tk):
    is_reset = True

    def __init__(self):
        super().__init__()
        self.geometry("720x465+760+200")
        self.resizable(True, True)
        self.title("Калькулятор СМО")
        self.icon = PhotoImage(file="smile.png")
        self.iconphoto(False, self.icon)

        self.notebook = ttk.Notebook()
        self.notebook.grid()

        self.frame1 = ttk.Frame(self.notebook)
        self.frame2 = ttk.Frame(self.notebook)
        self.frame3 = ttk.Frame(self.notebook)
        self.frame1.grid()
        self.frame2.grid()
        self.frame3.grid()
        self.notebook.add(self.frame1, text="Одноканальна СМО з відмовами")
        self.notebook.add(self.frame2, text="Багатоканальна СМО з відмовами")
        self.notebook.add(self.frame3, text="Одноканальна СМО з очікуванням з обмеженою чергою")


    def make_frame1(self):
        intensity_lambda_label = tk.Label(self.frame1, text="Введіть інтенсивність λ", font=("Helvetica", 14))
        intensity_lambda_label.grid(column=0, row=1)
        intensity_lambda_entry = ttk.Entry()
        intensity_lambda_entry.grid(column=1, row=1)
        intensity_miu_label = tk.Label(self.frame1, text="Введіть інтенсивність μ", font=("Helvetica", 14))
        intensity_miu_label.grid(column=0, row=2)
        intensity_miu_entry = ttk.Entry()
        intensity_miu_entry.grid(column=1, row=2)


    def run(self):
        self.make_frame1()
        self.mainloop()


if __name__ == '__main__':
    window = Window()
    window.run()

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Because the tk.Labels are in frame1, when you .grid() them in the frame, it puts them in the frame. When you put the Entry in root, it .grid()s them in root.

You can fix it by moving the Entry into frame1 when you create them like so:

ttk.Entry(self.frame1)

All in all it would look like this:

        intensity_lambda_label = tk.Label(self.frame1, text="Введіть інтенсивність λ", font=("Helvetica", 14))
        intensity_lambda_label.grid(column=0, row=1)
        intensity_lambda_entry = ttk.Entry(self.frame1)
        intensity_lambda_entry.grid(column=1, row=1)
        intensity_miu_label = tk.Label(self.frame1, text="Введіть інтенсивність μ", font=("Helvetica", 14))
        intensity_miu_label.grid(column=0, row=2)
        intensity_miu_entry = ttk.Entry(self.frame1)
        intensity_miu_entry.grid(column=1, row=2)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading