2012-01-17 123 views
3

使用在this stackoverflow post中找到的代碼,我稍微改變了它以包含每個其他checkbutton的背景色並設置寬度以填充Text小部件的寬度。但是,發生這種情況時,我無法使用鼠標滾輪進行滾動。我必須抓住滾動條。Tkinter checkbuttons在Text小部件中滾動

有沒有更好的方法來做到這一點,將允許正常滾動?這裏是我的代碼:

import Tkinter as tk 

root = tk.Tk() 
vsb = tk.Scrollbar(orient="vertical") 
text = tk.Text(root, width=40, height=20, yscrollcommand=vsb.set) 
vsb.config(command=text.yview) 
vsb.pack(side="right",fill="y") 
text.pack(side="top",fill="both",expand=True) 
for i in range(1000): 
    bg = 'grey' 
    if i % 2 == 0: 
     bg = 'white' 
    cb = tk.Checkbutton(text="checkbutton #%s" % i, bg=bg, width=35, justify=tk.LEFT) 
    text.window_create("end", window=cb) 
    text.insert("end", "\n") # to force one checkbox per line 

root.mainloop() 

回答

0
. 
. 
text.focus() # <---------- 
root.mainloop() 
+0

這似乎沒有幫助嗎?至少不是這個例子... – intargc 2012-01-27 18:27:08

1

的問題是,當你填寫了Checkbuttons文本控件,你現在用Checkbuttons'事件綁定當你的鼠標在窗口(而不是文本交互這是「隱藏」的小部件)。而Text小部件具有用於滾動鼠標的按鈕(按鈕4和5),Checkbuttons不會自動附帶。 (AFAIK)家長小部件不能自動設置爲通過他們的孩子接收任何事件,因此您必須手動完成。以下代碼將生效(在創建後cb後插入):

cb.bind('<Button-4>', lambda event: text.yview_scroll(-1, tk.UNITS)) 
cb.bind('<Button-5>', lambda event: text.yview_scroll(1, tk.UNITS)) 
相關問題