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

如何移动Tkinter ScrolledText组件并避免移位其他控件?

Tkinter ScrolledText 布局问题求助

我尝试将Tkinter的ScrolledText组件移至屏幕右侧,但不知如何操作;同时希望阻止该组件将其他所有控件向下移位。我刚接触Tkinter,对相关操作不太熟悉。

问题截图:
问题截图

我的代码如下:

user = POSMachine()
from tkinter import *
from tkinter.scrolledtext import *

pos = Tk()
pos.title("POS Machine")
pos.geometry("1000x550")

def priceListInput():
    option1Entry = Entry(pos)
    def priceList():
        user.setPriceList(option1Entry.get())
    setListButton = Button(pos, text = "Enter", command = lambda: [priceList(), 
removeOption1Widgets(option1Entry, setListButton)])
    option1Entry.grid(row = 1, column = 2)
    setListButton.grid(row = 1, column = 3)

def printList():
    option2Label = Label(pos, text = "{}".format(user.printPriceList()))
    option2Label.grid(row = 2, column = 1)

def addTransaction():
    idLabel = Label(pos, text = "Product ID: ")
    idEntry = Entry(pos)
    quantityLabel = Label(pos, text = "Quantity: ")
    quantityEntry = Entry(pos)
    enterdetails = Button(pos, text = "Enter")

    idLabel.grid(row = 3, column = 1) 
    idEntry.grid(row = 3, column = 2)
    quantityLabel.grid(row = 3, column = 3)
    quantityEntry.grid(row = 3, column = 4)
    enterdetails.grid(row = 3, column = 5)

def removeOption1Widgets(widget1, widget2):
    widget1.grid_remove()
    widget2.grid_remove()

def removeOption3Widgets():
    print("no")

def close():
    def exit():
        closeWindow.destroy()
        pos.destroy()
    def notExit():
        closeWindow.destroy()

    closeWindow = Tk()
    closeWindow.title("Close Window")
    closeWindow.geometry("300x100")

    label = Label(closeWindow, text = "Are you sure?")
    yesButton = Button(closeWindow, text = "Yes", command = exit, width = 10)
    noButton = Button(closeWindow, text = "No", command = notExit, width = 10)

    label.pack(side=TOP)
    yesButton.pack(side=TOP)
    noButton.pack(side=TOP)
                   
welcomeLabel = Label(pos, text = "Welcome to 7Evelyn")
option1Button = Button(pos, text = "Set Price List", command = priceListInput, width = 20)
option2Button = Button(pos, text = "View Price List", command = printList, width = 20)
option3Button = Button(pos, text = "New Transaction", command = addTransaction, width = 20,)
option4Button = Button(pos, text = "Sales Report", width = 20)
option5Button = Button(pos, text = "Close", command = close, width = 20)
display = ScrolledText(pos, width = 50)

welcomeLabel.grid(row = 0, column = 0)
option1Button.grid(row = 1, column = 0)
option2Button.grid(row = 2, column = 0)
option3Button.grid(row = 3, column = 0)
option4Button.grid(row = 4, column = 0)
option5Button.grid(row = 5, column = 0)
display.grid(row = 0, column = 10)

pos.mainloop()

解决方案

你的问题核心是grid布局的参数使用不当,以下是具体修改方案:

1. 修正ScrolledText的grid布局参数

把display.grid(row = 0, column = 10)替换为:

display.grid(row=0, column=1, rowspan=6, sticky="nsew")
  • column=1:直接将文本框放在左侧控件的右侧(左侧控件在column0),避免创建大量空列导致布局偏移。
  • rowspan=6:让文本框跨6行(覆盖row0到row5),刚好匹配左侧所有控件的高度范围,不会挤压下方按钮的位置。
  • sticky="nsew":让文本框填满分配的单元格空间,随窗口拉伸自动调整大小。

2. 设置布局权重,适配窗口大小

在创建主窗口后添加以下代码,让右侧区域自动扩展:

# 让右侧列(column1)随窗口宽度拉伸
pos.grid_columnconfigure(1, weight=1)
# 让所有行随窗口高度均匀调整
for i in range(6):
    pos.grid_rowconfigure(i, weight=1)

3. 优化左侧控件的显示效果(可选)

给左侧控件添加对齐方式和内边距,让布局更美观:

welcomeLabel.grid(row=0, column=0, sticky="n", padx=10, pady=10)
option1Button.grid(row=1, column=0, sticky="n", padx=10, pady=5)
option2Button.grid(row=2, column=0, sticky="n", padx=10, pady=5)
option3Button.grid(row=3, column=0, sticky="n", padx=10, pady=5)
option4Button.grid(row=4, column=0, sticky="n", padx=10, pady=5)
option5Button.grid(row=5, column=0, sticky="n", padx=10, pady=5)

修改后的完整代码:

user = POSMachine()
from tkinter import *
from tkinter.scrolledtext import *

pos = Tk()
pos.title("POS Machine")
pos.geometry("1000x550")

# 设置布局权重
pos.grid_columnconfigure(1, weight=1)
for i in range(6):
    pos.grid_rowconfigure(i, weight=1)

def priceListInput():
    option1Entry = Entry(pos)
    def priceList():
        user.setPriceList(option1Entry.get())
    setListButton = Button(pos, text = "Enter", command = lambda: [priceList(), 
removeOption1Widgets(option1Entry, setListButton)])
    option1Entry.grid(row = 1, column = 2)
    setListButton.grid(row = 1, column = 3)

def printList():
    option2Label = Label(pos, text = "{}".format(user.printPriceList()))
    option2Label.grid(row = 2, column = 1)

def addTransaction():
    idLabel = Label(pos, text = "Product ID: ")
    idEntry = Entry(pos)
    quantityLabel = Label(pos, text = "Quantity: ")
    quantityEntry = Entry(pos)
    enterdetails = Button(pos, text = "Enter")

    idLabel.grid(row = 3, column = 1) 
    idEntry.grid(row = 3, column = 2)
    quantityLabel.grid(row = 3, column = 3)
    quantityEntry.grid(row = 3, column = 4)
    enterdetails.grid(row = 3, column = 5)

def removeOption1Widgets(widget1, widget2):
    widget1.grid_remove()
    widget2.grid_remove()

def removeOption3Widgets():
    print("no")

def close():
    def exit():
        closeWindow.destroy()
        pos.destroy()
    def notExit():
        closeWindow.destroy()

    closeWindow = Tk()
    closeWindow.title("Close Window")
    closeWindow.geometry("300x100")

    label = Label(closeWindow, text = "Are you sure?")
    yesButton = Button(closeWindow, text = "Yes", command = exit, width = 10)
    noButton = Button(closeWindow, text = "No", command = notExit, width = 10)

    label.pack(side=TOP)
    yesButton.pack(side=TOP)
    noButton.pack(side=TOP)
                   
welcomeLabel = Label(pos, text = "Welcome to 7Evelyn")
option1Button = Button(pos, text = "Set Price List", command = priceListInput, width = 20)
option2Button = Button(pos, text = "View Price List", command = printList, width = 20)
option3Button = Button(pos, text = "New Transaction", command = addTransaction, width = 20,)
option4Button = Button(pos, text = "Sales Report", width = 20)
option5Button = Button(pos, text = "Close", command = close, width = 20)
display = ScrolledText(pos, width = 50)

# 调整左侧控件布局
welcomeLabel.grid(row=0, column=0, sticky="n", padx=10, pady=10)
option1Button.grid(row=1, column=0, sticky="n", padx=10, pady=5)
option2Button.grid(row=2, column=0, sticky="n", padx=10, pady=5)
option3Button.grid(row=3, column=0, sticky="n", padx=10, pady=5)
option4Button.grid(row=4, column=0, sticky="n", padx=10, pady=5)
option5Button.grid(row=5, column=0, sticky="n", padx=10, pady=5)
# 调整文本框布局
display.grid(row=0, column=1, rowspan=6, sticky="nsew")

pos.mainloop()

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.09 21:55:23