Tkinter 修复了框架的大小,使其不与滚动条重叠 Canvas 边框
Tkinter fix size of Frame to not overlap Canvas border with scrollbar
我正在使用 tkinter 开发 GUI,但遇到了问题。
当我向我的应用程序添加滚动条时,canvas 上的框架与轮廓重叠(见图)
代码如下:
from tkinter import *
window = Tk()
window.geometry("400x225")
scrollbar1 = Scrollbar(window, orient=VERTICAL)
canvas1 = Canvas(window, bg="#003333", yscrollcommand=scrollbar1.set)
frame1 = Frame(canvas1, bg="#003333")
scrollbar1.config(command=canvas1.yview)
scrollbar1.pack(side=RIGHT, fill=Y)
canvas1.pack(fill=BOTH, expand=True)
canvas1.create_window((0, 0), window=frame1, anchor="nw")
for x in range(20):
string = "line " + str(x)
label1 = Label(frame1, fg="white", bg="#003333", text=string, font=("Calibri Bold", 14))
label1.pack(pady=5)
window.update()
canvas1.config(scrollregion=canvas1.bbox("all"))
window.mainloop()
我不知道是否可行,但我希望框架适合 canvas 并保持轮廓。
我希望你能解决我的问题并且可能会帮助我!提前致谢。
Specifies a non-negative value indicating the width of the highlight rectangle to draw around the outside of the widget when it has the input focus.
所以,这并不是你真正想要的“边框”。它是在canvas中绘制space的一部分,当你使用window_create
绘制一个window时,那个window的父级是canvas,它在突出显示之前开始,因此 window 会滑过它。
一个解决方案,正如@martineau 所建议的那样,是通过指定 highlightthickness=0
来实现 0
并且正如您所建议的那样,您需要在整个事物周围设置“边框” ,您可以创建一个容器框架并指定 bd
参数,或者只设置 window window.config(bd=2)
.
的 bd
我正在使用 tkinter 开发 GUI,但遇到了问题。 当我向我的应用程序添加滚动条时,canvas 上的框架与轮廓重叠(见图)
代码如下:
from tkinter import *
window = Tk()
window.geometry("400x225")
scrollbar1 = Scrollbar(window, orient=VERTICAL)
canvas1 = Canvas(window, bg="#003333", yscrollcommand=scrollbar1.set)
frame1 = Frame(canvas1, bg="#003333")
scrollbar1.config(command=canvas1.yview)
scrollbar1.pack(side=RIGHT, fill=Y)
canvas1.pack(fill=BOTH, expand=True)
canvas1.create_window((0, 0), window=frame1, anchor="nw")
for x in range(20):
string = "line " + str(x)
label1 = Label(frame1, fg="white", bg="#003333", text=string, font=("Calibri Bold", 14))
label1.pack(pady=5)
window.update()
canvas1.config(scrollregion=canvas1.bbox("all"))
window.mainloop()
我不知道是否可行,但我希望框架适合 canvas 并保持轮廓。 我希望你能解决我的问题并且可能会帮助我!提前致谢。
Specifies a non-negative value indicating the width of the highlight rectangle to draw around the outside of the widget when it has the input focus.
所以,这并不是你真正想要的“边框”。它是在canvas中绘制space的一部分,当你使用window_create
绘制一个window时,那个window的父级是canvas,它在突出显示之前开始,因此 window 会滑过它。
一个解决方案,正如@martineau 所建议的那样,是通过指定 highlightthickness=0
来实现 0
并且正如您所建议的那样,您需要在整个事物周围设置“边框” ,您可以创建一个容器框架并指定 bd
参数,或者只设置 window window.config(bd=2)
.
bd