I have labels FirstName, LastName, and License. In my program, I wanted them justified to the left but I want them flush with the input boxes below them. Currently, they are shifted a little further left. I'm not having much luck moving them over just a tad. Any suggestions?
import os
import pypyodbc
import tkinter
from tkinter import ttk
from tkinter import messagebox
class Adder(ttk.Frame):
"""The adders gui and functions."""
def __init__(self, parent, *args, **kwargs):
ttk.Frame.__init__(self, parent, *args, **kwargs)
self.root = parent
self.init_gui()
def on_help(self):
answer = messagebox.showinfo("Stuff goes here.")
def on_quit(self):
"""Exits program."""
root.quit()
def calculate(self):
if len(self.lic_entry.get()) == 0:
self.output['text'] = "Output will go here.\nThis is a test."
def init_gui(self):
"""Builds GUI."""
self.root.title('Verify')
self.root.option_add('*tearOff', 'FALSE')
self.grid(column=0, row=0, sticky='nsew') # this starts the entire form
self.menubar = tkinter.Menu(self.root)
self.menu_file = tkinter.Menu(self.menubar)
self.menu_file.add_command(label='About', command=self.on_help)
self.menu_file.add_command(label='Exit', command=self.on_quit)
self.menu_edit = tkinter.Menu(self.menubar)
self.menubar.add_cascade(menu=self.menu_file, label='File')
# self.menubar.add_cascade(menu=self.menu_edit, label='Help') # add other menu options
self.root.config(menu=self.menubar)
# Text Labels
self.first = tkinter.Label(self, text='FirstName:')
self.first.grid(column=0, row=1, sticky='w')
self.last = ttk.Label(self, text='LastName:')
self.last.grid(column=1, row=1, sticky='w')
self.lic = ttk.Label(self, text='License:')
self.lic.grid(column=2, row=1, sticky='w')
# Input Boxes and Button
self.first_entry = tkinter.Entry(self, width=28) # first input box
self.first_entry.grid(sticky='e', column=0, row=2)
self.last_entry = tkinter.Entry(self, width=28) # second input box
self.last_entry.grid(sticky='e', column=1, row=2)
self.lic_entry = tkinter.Entry(self, width=28) # third input box
self.lic_entry.grid(sticky='e', column=2, row=2)
self.framespace = tkinter.Frame(self, height=10, width=600) # provides spacing between input boxes and button
self.framespace.grid(column=0, row=4, columnspan=5)
self.calc_button = ttk.Button(self, text='Search', command=self.calculate) # button
self.calc_button.grid(column=0, row=5, columnspan=1, sticky='w')
# Output frame for answers
self.output = tkinter.LabelFrame(self, height=200, width=600, bg='#F7F7F7', text=' ', bd=0, labelanchor='n')
self.output.grid(column=0, row=6, columnspan=5)
for child in self.winfo_children(): # padx 10 adds horizontal padding on the out edge of window
child.grid_configure(padx=0, pady=0)
if __name__ == '__main__':
root = tkinter.Tk()
Adder(root)
root.resizable(width=False, height=False) # locks window from being resized
root.mainloop()
You can use the padding
option of the label to add padding inside the borders of the widget.
From the documentation:
Specifies the amount of extra space to allocate for the widget. The padding is a list of up to four length specifications left top right bottom. If fewer than four elements are specified, bottom defaults to top, right defaults to left, and top defaults to left.
For example:
self.last = ttk.Label(..., padding=(2,0,0,0))
You could instead use padx
when you call grid:
self.last.grid(..., padx=20)
However, the last won't seem to make a difference because later on in your code you reset the padx and pady values to zero. You will need to remove that code for padx
to have an effect here.