以下是我正在做的事情的一个示例。我在滑动画布上放了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()
答案 0 :(得分:1)
问题是scrollregion
实例上的Canvas
不够广泛。您可以根据所有按钮的请求宽度计算需要的宽度,然后在完成所有按钮后设置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()
作为旁注,前两个导入语句是多余的。在这个例子中,我删除了第二个,以避免污染全局命名空间。