如何在 Tkinter/TTK 中将标签中的文本移动一点?
How to shift the text in a label over a little in Tkinter/TTK?
我有 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()
您可以使用标签的 padding
选项在小部件的边框内添加填充。
来自文档:
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.
例如:
self.last = ttk.Label(..., padding=(2,0,0,0))
您可以在调用 grid:
时改用 padx
self.last.grid(..., padx=20)
但是,最后一个似乎没有什么不同,因为稍后在您的代码中您将 padx 和 pady 值重置为零。您需要删除 padx
的代码才能在此处生效。
我有 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()
您可以使用标签的 padding
选项在小部件的边框内添加填充。
来自文档:
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.
例如:
self.last = ttk.Label(..., padding=(2,0,0,0))
您可以在调用 grid:
时改用padx
self.last.grid(..., padx=20)
但是,最后一个似乎没有什么不同,因为稍后在您的代码中您将 padx 和 pady 值重置为零。您需要删除 padx
的代码才能在此处生效。