我有标签FirstName,LastName和License。 在我的程序中,我希望它们向左对齐,但我希望它们与它们下方的输入框齐平。目前,它们向左移动了一点。我没有太多的运气让他们过一点点。有什么建议吗?
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()
答案 0 :(得分:1)
您可以使用标签的padding
选项在窗口小部件的边框内添加填充。
来自文档:
指定要为窗口小部件分配的额外空间量。填充是左上角右下角最多四个长度规格的列表。如果指定的元素少于四个,则默认为top,right默认为left,top默认为left。
例如:
self.last = ttk.Label(..., padding=(2,0,0,0))
您可以在调用grid时使用padx
:
self.last.grid(..., padx=20)
但是,最后一个似乎没有区别,因为稍后在代码中将padx和pady值重置为零。您需要删除padx
的代码才能生效。