gpt4 book ai didi

python - Tkinter,我无法让 slider 显示所有按钮

转载 作者:太空宇宙 更新时间:2023-11-03 16:50:03 25 4
gpt4 key购买 nike

这是我正在做的事情的示例。我在滑动 Canvas 上放置了 500 个按钮,但我只能滚动到按钮 109。我做错了什么?我尝试调整小部件的宽度以及滚动区域。我确信我忽略了一些简单的事情。

import Tkinter
from Tkinter import *
import os, signal

window1 = Tkinter.Tk()
window1.attributes('-fullscreen', True)
window1.bind("<Escape>", lambda e: e.widget.quit())

window2 = Tkinter.Label(window1)
window2.configure(bg = "blue")
window2.place(x=0, y=0, relwidth=1, relheight=1)

frame=Frame(window2, height=160)
frame.pack(anchor=Tkinter.SW,side=Tkinter.BOTTOM)

canvas=Canvas(frame,bg='black', width=5500, height=160, scrollregion=(0,0,5500,0))

hbar=Scrollbar(frame,orient=HORIZONTAL, bg='black', troughcolor='black')
hbar.pack(side=BOTTOM,fill=X)
hbar.config(command=canvas.xview)

canvas.config(xscrollcommand=hbar.set, xscrollincrement=4.5)
canvas.pack(side=LEFT,expand=True,fill=BOTH)
frm = Frame(canvas)
canvas.create_window(0, 0, window=frm, anchor='nw')

def quit():
window1.quit()

count = 0
while (count < 500):
X = Tkinter.Button(frm, text=str(count), height=6, width=5, padx=0, pady=0, highlightcolor="blue", highlightthickness=3, command=quit)
X.pack(side = "left")
count = count + 1

window1.mainloop()

最佳答案

问题是 Canvas 实例上的 scrollregion 不够宽。您可以根据所有按钮请求的宽度来计算它需要多宽,然后在制作完所有按钮后设置 scrollregion,如下所示:

import Tkinter
import os, signal

window1 = Tkinter.Tk()
window1.attributes('-fullscreen', True)
window1.bind("<Escape>", lambda e: e.widget.quit())

window2 = Tkinter.Label(window1)
window2.configure(bg = "blue")
window2.place(x=0, y=0, relwidth=1, relheight=1)

frame = Tkinter.Frame(window2, height=160)
frame.pack(anchor=Tkinter.SW,side=Tkinter.BOTTOM)

canvas=Tkinter.Canvas(frame,bg='black', width=5500, height=160)

hbar=Tkinter.Scrollbar(frame,orient=Tkinter.HORIZONTAL, bg='black', troughcolor='black')
hbar.pack(side=Tkinter.BOTTOM,fill=Tkinter.X)
hbar.config(command=canvas.xview)

canvas.config(xscrollcommand=hbar.set, xscrollincrement=4.5)
canvas.pack(side=Tkinter.LEFT, expand=True, fill=Tkinter.BOTH)
frm = Tkinter.Frame(canvas)
canvas.create_window(0, 0, window=frm, anchor='nw')

def quit():
window1.quit()

count = 0
width = 0
while (count < 500):
X = Tkinter.Button(frm, text=str(count), height=6, width=5, padx=0, pady=0, highlightcolor="blue", highlightthickness=3, command=quit)
X.pack(side = "left")
width += X.winfo_reqwidth()
count = count + 1

canvas.config(scrollregion=(0, 0, width, 0))
window1.mainloop()

顺便说一句,前两个 import 语句是多余的。在此示例中,我删除了第二个以避免污染全局命名空间。

关于python - Tkinter,我无法让 slider 显示所有按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35922839/

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