gpt4 book ai didi

整个窗口的 Python Tkinter 滚动条

转载 作者:行者123 更新时间:2023-12-05 02:23:39 27 4
gpt4 key购买 nike

有没有一种方法可以在我的整个窗口中添加滚动条,而无需将所有内容都放入框架中?我已经使用 .grid 设置了所有内容,但我不喜欢将所有内容都包裹起来的想法。

root = Tk()
root.maxsize(900,600)

circus()#calls the function to set up everything

root.mainloop()

最佳答案

如何在 tkinter 中将滚动条添加到整个窗口?

这是 python 3 的答案...


from tkinter import *

from tkinter import ttk



root = Tk()

root.title('Full Window Scrolling X Y Scrollbar Example')

root.geometry("1350x400")



# Create A Main frame

main_frame = Frame(root)

main_frame.pack(fill=BOTH,expand=1)



# Create Frame for X Scrollbar

sec = Frame(main_frame)

sec.pack(fill=X,side=BOTTOM)



# Create A Canvas

my_canvas = Canvas(main_frame)

my_canvas.pack(side=LEFT,fill=BOTH,expand=1)



# Add A Scrollbars to Canvas

x_scrollbar = ttk.Scrollbar(sec,orient=HORIZONTAL,command=my_canvas.xview)

x_scrollbar.pack(side=BOTTOM,fill=X)

y_scrollbar = ttk.Scrollbar(main_frame,orient=VERTICAL,command=my_canvas.yview)
y_scrollbar.pack(side=RIGHT,fill=Y)



# Configure the canvas

my_canvas.configure(xscrollcommand=x_scrollbar.set)

my_canvas.configure(yscrollcommand=y_scrollbar.set)

my_canvas.bind("<Configure>",lambda e: my_canvas.config(scrollregion= my_canvas.bbox(ALL)))



# Create Another Frame INSIDE the Canvas

second_frame = Frame(my_canvas)



# Add that New Frame a Window In The Canvas

my_canvas.create_window((0,0),window= second_frame, anchor="nw")





for thing in range(100):

Button(second_frame ,text=f"Button {thing}").grid(row=5,column=thing,pady=10,padx=10)



for thing in range(100):

Button(second_frame ,text=f"Button {thing}").grid(row=thing,column=5,pady=10,padx=10)



root.mainloop()

关于整个窗口的 Python Tkinter 滚动条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19860047/

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