You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Tkinter中将Label与TextBox放置在NW角并紧邻NE角的Frame?

Tkinter 界面布局调整方案

要实现左上角的Label/TextBox与右上角的Frame紧邻排列,核心是将左侧控件统一放入一个容器Frame,再通过pack布局的side参数让左右两个容器并排显示。以下是修改后的代码:

from tkinter import *
from tkinter import filedialog
import os

root = Tk()
root.title("")
root.iconbitmap("")
root.geometry("400x400")
root["bg"] = "Cyan"

# 左侧容器:存放XPATH相关控件
left_frame = Frame(root, bg='Cyan')
left_frame.pack(side=LEFT, anchor=NW, padx=5, pady=5)

LabelXP1 = Label(left_frame, bg='Cyan', width=10, height=1, text="XPATH 1:")
LabelXP1.pack(anchor=NW)
xpath1 = Text(left_frame, width=20, height=1)
xpath1.pack(anchor=NW)

LabelXP2 = Label(left_frame, bg='Cyan', width=10, height=1, text="XPATH 2:")
LabelXP2.pack(anchor=NW)
xpath2 = Text(left_frame, width=20, height=1)
xpath2.pack(anchor=NW)

# 右侧容器:存放列表相关控件
list_frame = Frame(root, background="Cyan")
list_frame.pack(side=RIGHT, anchor=NE, padx=5, pady=5)

Label1 = Label(list_frame, background='Cyan', width=6, height=1, text="Urls: 0")
Label1.pack(anchor=N)

scrollbar = Scrollbar(list_frame, orient=VERTICAL)
scrollbarh = Scrollbar(list_frame, orient=HORIZONTAL)

listbox = Listbox(list_frame, background="Cyan", width=20, yscrollcommand=scrollbar.set, xscrollcommand=scrollbar.set)
scrollbar.config(command=listbox.yview)
scrollbar.pack(side=RIGHT, fill=Y)

scrollbarh.config(command=listbox.xview)
scrollbarh.pack(side=BOTTOM, fill=X)

listbox.pack(pady=20)

root.mainloop()

关键改动说明:

  • 创建left_frame作为左侧控件的容器,将所有XPATH相关Label和Text放入其中
  • 对left_frame使用pack(side=LEFT, anchor=NW)实现左对齐
  • 对右侧的list_frame使用pack(side=RIGHT, anchor=NE)实现右对齐
  • 添加padx和pady参数调整控件间的间距,避免过于拥挤

这样调整后,左侧的XPATH控件组会与右侧的列表Frame在顶部紧邻排列,符合预期布局效果。

内容的提问来源于stack exchange,提问作者spike666spike666

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.09 16:47:43