gpt4 book ai didi

python - 如何在 Tkinter/TTK 中稍微移动标签中的文本?

转载 作者:太空宇宙 更新时间:2023-11-03 15:17:00 24 4
gpt4 key购买 nike

我有名字、姓氏和许可证标签。在我的程序中,我希望它们向左对齐,但我希望它们与下面的输入框齐平。目前,它们向左移动了一点。我运气不太好,只能把它们移动一点点。有什么建议吗?

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 的代码才能在此处生效。

关于python - 如何在 Tkinter/TTK 中稍微移动标签中的文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43806045/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com