无法使用滚动条正确调整框架的大小,让我们说屏幕的全宽
Cannot properly size Frame with scrollbar to lets say full width of screen
我的问题是我无法将程序内容的框架适当地调整为全屏宽度或根本无法更改它。我得到的只是一个大根 window 和一个小 window 锚定在它的左上角,里面有我的内容。但我希望我的内容能够很长,可能会从显示的一侧到另一侧。
from tkinter import *
def update_scrollregion(event):
photoCanvas.configure(scrollregion=photoCanvas.bbox("all"))
root = Tk()
root.geometry("1700x900")
#i tried to change the width below without success, it doesnt change anything either gets smaller nor bigger
photoFrame = Frame(root, width=250, height=190, bg="#EBEBEB")
photoFrame.grid()
photoFrame.rowconfigure(0, weight=1)
photoFrame.columnconfigure(0, weight=1)
photoCanvas = Canvas(photoFrame, bg="#EBEBEB")
photoCanvas.grid(row=0, column=0, sticky="nsew")
canvasFrame = Frame(photoCanvas, bg="#EBEBEB")
photoCanvas.create_window(0, 0, window=canvasFrame, anchor='nw')
txt=Text(canvasFrame, height=2, width=30)
Button(canvasFrame, text="Button1", borderwidth=0, bg="#EBEBEB").grid(row=1, column=1, padx=5, pady=5, sticky="nsew")
txt.insert(INSERT, "imagine a very long text here which should stretch from the left side of my screen to the right side of it")
txt.grid(row=1, column=2, padx=5, pady=5)
photoScroll = Scrollbar(photoFrame, orient=VERTICAL)
photoScroll.config(command=photoCanvas.yview)
photoCanvas.config(yscrollcommand=photoScroll.set)
photoScroll.grid(row=0, column=1, sticky="ns")
canvasFrame.bind("<Configure>", update_scrollregion)
root.mainloop()```
您需要添加 .. root.rowconfigure(0, weight=1) 和 root.columnconfigure(0, weight=1) 并设置 photoFrame.grid(sticky=NSEW)。
from tkinter import *
def update_scrollregion(event):
photoCanvas.configure(scrollregion=photoCanvas.bbox("all"))
root = Tk()
root.geometry("1700x900")
root.rowconfigure(0, weight=1)
root.columnconfigure(0, weight=1)
#i tried to change the width below without success, it doesnt change anything either gets smaller nor bigger
photoFrame = Frame(root, width=250, height=190, bg="#EBEBEB")
photoFrame.rowconfigure(0, weight=1)
photoFrame.columnconfigure(0, weight=1)
photoFrame.grid(sticky=NSEW)
photoCanvas = Canvas(photoFrame, bg="#EBEBEB")
photoCanvas.rowconfigure(0, weight=1)
photoCanvas.columnconfigure(0, weight=1)
photoCanvas.grid(row=0, column=0, sticky="nsew")
canvasFrame = Frame(photoCanvas, bg="#EBEBEB")
canvasFrame.rowconfigure(1, weight=1)
canvasFrame.columnconfigure(2, weight=1)
canvasFrame.grid(sticky=NSEW)
photoCanvas.create_window(0, 0, window=canvasFrame, anchor='nw')
# txt = Text(canvasFrame, height=2, width=30)
txt = Text(canvasFrame)
txt.insert(INSERT, "imagine a very long text here which should stretch from the left side of my screen to the right side of it, imagine a very long text here which should stretch from the left side of my screen to the right side of it")
txt.grid(row=1, column=2, padx=5, pady=5, sticky=NSEW)
Button(canvasFrame, text="Button1", borderwidth=0, bg="#EBEBEB").grid(row=1, column=1, padx=5, pady=5, sticky="nsew")
photoScroll = Scrollbar(photoFrame, orient=VERTICAL)
photoScroll.config(command=photoCanvas.yview)
photoCanvas.config(yscrollcommand=photoScroll.set)
photoScroll.grid(row=0, column=1, sticky="ns")
canvasFrame.bind("<Configure>", update_scrollregion)
root.mainloop()
我的问题是我无法将程序内容的框架适当地调整为全屏宽度或根本无法更改它。我得到的只是一个大根 window 和一个小 window 锚定在它的左上角,里面有我的内容。但我希望我的内容能够很长,可能会从显示的一侧到另一侧。
from tkinter import *
def update_scrollregion(event):
photoCanvas.configure(scrollregion=photoCanvas.bbox("all"))
root = Tk()
root.geometry("1700x900")
#i tried to change the width below without success, it doesnt change anything either gets smaller nor bigger
photoFrame = Frame(root, width=250, height=190, bg="#EBEBEB")
photoFrame.grid()
photoFrame.rowconfigure(0, weight=1)
photoFrame.columnconfigure(0, weight=1)
photoCanvas = Canvas(photoFrame, bg="#EBEBEB")
photoCanvas.grid(row=0, column=0, sticky="nsew")
canvasFrame = Frame(photoCanvas, bg="#EBEBEB")
photoCanvas.create_window(0, 0, window=canvasFrame, anchor='nw')
txt=Text(canvasFrame, height=2, width=30)
Button(canvasFrame, text="Button1", borderwidth=0, bg="#EBEBEB").grid(row=1, column=1, padx=5, pady=5, sticky="nsew")
txt.insert(INSERT, "imagine a very long text here which should stretch from the left side of my screen to the right side of it")
txt.grid(row=1, column=2, padx=5, pady=5)
photoScroll = Scrollbar(photoFrame, orient=VERTICAL)
photoScroll.config(command=photoCanvas.yview)
photoCanvas.config(yscrollcommand=photoScroll.set)
photoScroll.grid(row=0, column=1, sticky="ns")
canvasFrame.bind("<Configure>", update_scrollregion)
root.mainloop()```
您需要添加 .. root.rowconfigure(0, weight=1) 和 root.columnconfigure(0, weight=1) 并设置 photoFrame.grid(sticky=NSEW)。
from tkinter import *
def update_scrollregion(event):
photoCanvas.configure(scrollregion=photoCanvas.bbox("all"))
root = Tk()
root.geometry("1700x900")
root.rowconfigure(0, weight=1)
root.columnconfigure(0, weight=1)
#i tried to change the width below without success, it doesnt change anything either gets smaller nor bigger
photoFrame = Frame(root, width=250, height=190, bg="#EBEBEB")
photoFrame.rowconfigure(0, weight=1)
photoFrame.columnconfigure(0, weight=1)
photoFrame.grid(sticky=NSEW)
photoCanvas = Canvas(photoFrame, bg="#EBEBEB")
photoCanvas.rowconfigure(0, weight=1)
photoCanvas.columnconfigure(0, weight=1)
photoCanvas.grid(row=0, column=0, sticky="nsew")
canvasFrame = Frame(photoCanvas, bg="#EBEBEB")
canvasFrame.rowconfigure(1, weight=1)
canvasFrame.columnconfigure(2, weight=1)
canvasFrame.grid(sticky=NSEW)
photoCanvas.create_window(0, 0, window=canvasFrame, anchor='nw')
# txt = Text(canvasFrame, height=2, width=30)
txt = Text(canvasFrame)
txt.insert(INSERT, "imagine a very long text here which should stretch from the left side of my screen to the right side of it, imagine a very long text here which should stretch from the left side of my screen to the right side of it")
txt.grid(row=1, column=2, padx=5, pady=5, sticky=NSEW)
Button(canvasFrame, text="Button1", borderwidth=0, bg="#EBEBEB").grid(row=1, column=1, padx=5, pady=5, sticky="nsew")
photoScroll = Scrollbar(photoFrame, orient=VERTICAL)
photoScroll.config(command=photoCanvas.yview)
photoCanvas.config(yscrollcommand=photoScroll.set)
photoScroll.grid(row=0, column=1, sticky="ns")
canvasFrame.bind("<Configure>", update_scrollregion)
root.mainloop()