Skip to main content
 首页 » 编程设计

python之如何在顶级窗口python上创建垂直和水平滚动条

2024年12月31日7haluo1

我是 Python 新手。 我在帖子的帮助下创建了垂直和水平滚动条。 当我调整顶层窗口的大小时,滚动条不会移动。 如何使滚动条随着调整窗口大小而移动。谢谢

import tkinter as tk 
import tkinter.ttk as ttk 
from tkinter import * 
 
root = Tk() 
 
top=Toplevel(root) 
text = Text(top) 
vs = Scrollbar(top, orient="vertical") 
hs = Scrollbar(top, orient="horizontal") 
sizegrip = ttk.Sizegrip(root) 
 
# hook up the scrollbars to the text widget 
text.configure(yscrollcommand=vs.set, xscrollcommand=hs.set, wrap="none") 
vs.configure(command=text.yview) 
hs.configure(command=text.xview) 
 
# grid everything on-screen 
text.grid(row=0,column=0,sticky="news") 
vs.grid(row=0,column=1,sticky="ns") 
hs.grid(row=1,column=0,sticky="news") 
sizegrip.grid(row=1,column=1,sticky="news") 
root.grid_rowconfigure(0, weight=1) 
root.grid_columnconfigure(0, weight=1) 
 
def fill(): 
    for n in range(30): 
        text.insert("end", 'check scroll bar ' * 20, "", "\n") 
 
 
Button(root, text='press', command=fill).grid(row=0, column=0) 
root.mainloop() 

请您参考如下方法:

你需要告诉 Toplevel 在调整窗口大小时调整 row=0, column=0 的单元格:

# grid everything on-screen 
top.columnconfigure(0, weight=1) 
top.rowconfigure(0, weight=1)