2016-03-10 65 views
1

下面是我正在做的一個示例。我在滑動畫布上放置了500個按鈕,而且我只能滾動到按鈕109.我在做什麼錯了?我試着調整窗口小部件的寬度,以及scrollregion。我確信我忽略了一些簡單的東西。Tkinter,我無法讓我的滑塊顯示所有按鈕

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() 

回答

1

的問題是,你scrollregionCanvas實例不夠寬。你可以計算出它需要多寬是基於所有按鈕的請求寬度,然後設置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() 

作爲一個側面說明,你的第一個兩個進口報表是多餘的。在這個例子中,我放棄了第二個,以避免污染全局命名空間。