tkinter 初学者:如何将标签放置在条目文本框的左侧

问题描述 投票:0回答:1

这是我第一次编写 GUI 程序。我希望将标签放在输入文本框的左侧,并在 GUI 的中心对齐,但我不知道该怎么做,任何人都可以提供帮助,将不胜感激

(注意:这是一个无功能的 GUI 示例。我没有添加任何有关文本框或按钮如何工作的代码。稍后将添加它们)

import tkinter as tk

root = tk.Tk()
root.geometry("400x400")
root.configure(bg='White')
root.title("Search your class information")

# Create a label with background and foreground colors
label = tk.Label(root, text="Search your for your class using one or more of the search criteria", bg="white", fg="black")
label.pack()

label = tk.Label(root, text="Enter Text:")
label.pack()

entry = tk.Entry(root, selectbackground="lightblue", selectforeground="black")
entry.pack()

# Create a button with active background and foreground colors
button = tk.Button(root, text="Search", activebackground="blue", activeforeground="white")
button.pack()

root.mainloop()
python tkinter spyder
1个回答
0
投票

只需将标签和条目放入框架中即可:

import tkinter as tk

root = tk.Tk()
root.geometry("400x400")
root.configure(bg='White')
root.title("Search your class information")

# Create a label with background and foreground colors
label = tk.Label(root, text="Search your for your class using one or more of the search criteria", bg="white", fg="black")
label.pack()

# frame for the label and entry box
frame = tk.Frame(root)
frame.pack()

# created as child of the above frame
label = tk.Label(frame, text="Enter Text:")
label.pack(side='left')

# created as child of above frame
entry = tk.Entry(frame, selectbackground="lightblue", selectforeground="black")
entry.pack(side='left')

# Create a button with active background and foreground colors
button = tk.Button(root, text="Search", activebackground="blue", activeforeground="white")
button.pack()

root.mainloop()

输出

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.