2016-02-20 73 views
0

在下面的程序中,我顯示一個輸入框,我希望能夠在我的輸入框下方有一個滾動條。這工作,但滾動條在底部,而不是在輸入框下。如何設置Entry Scollbar的位置?

from tkinter import * 

root = Tk() 
calc = Frame(root) 
calc.grid() 

def saveIt(): 
    print(text_box.get()) 

text_box = Entry(calc, justify=RIGHT, width=20) 
text_box.grid(row=1, column = 0, pady = 5) 
text_box.insert(0, "testtest1234567890testtest") 

scrollbar=Scrollbar(root,orient=HORIZONTAL, command=text_box.xview, width=10) 
scrollbar.grid(row=2, column = 0,sticky=E+W, pady=4) 

save_button = Button(calc, text = "save") 
save_button["command"] = lambda: saveIt() 
save_button.grid(row=3, column = 0, pady = 5) 

text_box.config(xscrollcommand=scrollbar.set) 
root.mainloop() 

因爲我剛開始學習Python和Tkinter的,任何和所有的意見(甚至是解決問題的方法)將領受。

預先感謝您。

+0

此外,沒有必要'拉姆達:saveIt()'。你可以(也應該)只寫'save_button ['command'] = saveIt'。 – gil

回答

2

這是因爲text_boxsave_button父窗口部件爲calc,其母公司爲root和滾動條的父也root。因此calc作爲一個整體在滾動條上方網格化,因此calc中的所有內容都在滾動條上方。你只需要改變

scrollbar = Scrollbar(root, orient=HORIZONTAL, command=text_box.xview, width=10) 

scrollbar = Scrollbar(calc, orient=HORIZONTAL, command=text_box.xview, width=10)