user1332577
user1332577

Reputation: 362

set Tkinter label widget below entry widget

I am relatively new to Tkinter and I am trying to create a GUI with three horizontally aligned entry widgets and three corresponding label widgets. I am struggling with how to put the label widgets below the entry widgets without disturbing the alignment of the entry boxes (i.e., pack side = LEFT). Here is an example snapshot of the GUI without the labels below the entry boxes:

enter image description here

Currently my script looks like this, with the label widgets commented out:

#!/usr/bin/python

from sys import exit
from Tkinter import *
from subprocess import Popen, PIPE
from time import sleep
from os.path import exists
import os
import time
import sys
import ttk

#Initialize Tkinter by creating Tk root widget, which is a window with a
title bar. The root widget has to be created before any other widgets.

Root = Tk()
Root.title("GUI")

class Command:
    def __init__(self, func, *args, **kw):
        self.func = func
        self.args = args
        self.kw = kw
    def __call__(self, *args, **kw):
        args = self.args+args
        kw.update(self.kw)
        self.func(*args, **kw)

 #Script definitions#

 def quitter():
    exit(0)


#Button organization and layout#


Sub0 = Frame(Root)
Sub = Frame(Sub0)

#X mesh node
Label(Sub, text = "Create x mesh nodes").pack(side = TOP, pady = 5)

#x min text box area
v1=StringVar()
#Create entry widge to get user input, in this case a text string for the x      
min mesh

Entry(Sub, textvariable=v1).pack(side = LEFT, pady = 5, padx = 5)
#Label(Sub, text = "min").pack(side = BOTTOM, pady = 5, padx = 5)


#x max text box area
v2=StringVar()
Entry(Sub, textvariable=v2).pack(side = LEFT, pady = 5, padx = 5)
#Label(Sub, text = "max").pack(side = BOTTOM, pady = 1)

#x step text box area
v3=StringVar()
Entry(Sub, textvariable=v3).pack(side = LEFT, pady = 5, padx = 5)
#Label(Sub, text = "step increment").pack(side = LEFT, pady = 1)

Sub.pack(side = TOP)
Frame(Sub0,height=1, relief = GROOVE, bg="black").pack(side = TOP, fill = 
BOTH, pady= 5)


#GUI Organization#


Sub0.pack(side = TOP, padx = 10)

Button(Root, text = "Quit", command = quitter).pack(side = TOP, pady = 5)

#The window will not appear until we enter the Tkinter event loop. Script
will remain in the event loop until we close the window.

Root.mainloop()

When I uncomment the label widgets I get the following:

enter image description here

Can someone guide me in the right direction on how to make this happen? Or suggest a better way to implement it? Also, is there a way to control the size of the entry widgets? I'd like them to be smaller. Thank you in advance!

Upvotes: 0

Views: 2672

Answers (1)

Pythonista
Pythonista

Reputation: 11615

You can use the grid geometry manager for this:

from sys import exit
from Tkinter import *
from subprocess import Popen, PIPE
from time import sleep
from os.path import exists
import os, time, sys, ttk

#Initialize Tkinter by creating Tk root widget, which is a window with a
Root = Tk()
Root.title("VIZ TOOL")

class Command:
    def __init__(self, func, *args, **kw):
        self.func = func
        self.args = args
        self.kw = kw
    def __call__(self, *args, **kw):
        args = self.args+args
        kw.update(self.kw)
        self.func(*args, **kw)

 #Script definitions#

def quitter():
    exit(0)


#Button organization and layout#


Sub0 = Frame(Root)
Sub = Frame(Sub0)

#X mesh node
Label(Sub, text = "Create x mesh nodes").grid(columnspan=3)

#x min text box area
v1=StringVar()
#Create entry widge to get user input, in this case a text string for the x      

Entry(Sub, textvariable=v1).grid(row=1, column=0)
Label(Sub, text = "min").grid(row=2, column=0)


#x max text box area
v2=StringVar()
Entry(Sub, textvariable=v2).grid(row=1, column=1)
Label(Sub, text = "max").grid(row=2, column=1)
#x step text box area
v3=StringVar()
Entry(Sub, textvariable=v3).grid(row=1, column=2)
Label(Sub, text = "step increment").grid(row=2, column=2)

Sub.pack(side = TOP)
Frame(Sub0,height=1, relief = GROOVE, bg="black").pack(side = TOP, fill = 
BOTH, pady= 5)


#GUI Organization#


Sub0.pack(side = TOP, padx = 10)

Button(Root, text = "Quit", command = quitter).pack(side = TOP, pady = 5)

#The window will not appear until we enter the Tkinter event loop. Script

Root.mainloop()

For the size of the entries you can do something like this:

Root.option_add("*Entry.Width", 5) #10 is default

Using option add like this will override the size of all entry widgets, you can specify individual size with the width keyword on Entry(....., width = *, ...)

Upvotes: 3

Related Questions