我正在玩弄Tkinter窗口小部件,並試圖將滾動條應用到畫布窗口小部件,但是當程序運行時會掛起 - 沒有錯誤消息等。將滾動條應用到Tkinter中的畫布窗口小部件的問題
首先是附加滾動條的一般邏輯好嗎?
其次,問題似乎與.pack()
方法一樣,因爲刪除它可以讓程序運行。正如我已經使用.grid()
方法,否則這意味着我必須使用滾動的.grid()
方法?如果是的話,如何實現這一目標?任何幫助實現這一目標都將受到極大的重視。預先感謝您的時間。
from tkinter import *
x = 10
y = 10
a = 100
b = 100
def change_coord(event):
global coord
if event.keysym == 'Up':
coord[1] -=1
coord[3] -=3
if event.keysym == 'Down':
coord[1] +=1
coord[3] +=3
if event.keysym == 'Right':
coord[0] +=1
coord[2] +=3
if event.keysym == 'Left':
coord[0] -=1
coord[2] -=3
canvas1.coords(arc, *coord)
window = Tk()
window.geometry("500x500")
#canvas scroll bar
scrollbar = Scrollbar(window)
scrollbar.pack(side=RIGHT, fill=Y)
#canvas and drawing
canvas1=Canvas(window, yscrollcommand=scrollbar.set, height = 200, width = 400)
canvas1.grid(row=0, column=0, sticky=W)
coord = [x, y, a, b]
arc = canvas1.create_rectangle(*coord, outline="#fb0", fill="#fb0")
#canvas scrollbar continued
scrollbar.config(command=canvas1.yview)
#captureing keyboard inputs and assigning to function
window.bind_all('<Up>', change_coord)
window.bind_all('<Down>', change_coord)
window.bind_all('<Left>', change_coord)
window.bind_all('<Right>', change_coord)
window.mainloop()
另外,我看到,當我使用.pack()
方法放置在畫布上的程序運行窗口,滾動條是可見的,但滾動條不實際工作。任何有關這個問題的幫助都會非常受歡迎。謝謝。
爲什麼_are_使用'grid',而不是'pack'爲'canvas1'? – abarnert 2014-09-28 11:02:35
此代碼取自所有添加的小部件已使用網格方法組織的代碼的較大部分。 – sw123456 2014-09-28 11:03:49
從http://effbot.org/tkinterbook/grid.htm:警告:切勿在同一個主窗口中混合網格和包。順便說一句,對我來說,這有些作用(用「包」的畫布)。我有一個滾動條,可以通過單擊上/下框來移動框。然而,酒吧本身則充滿了整個垂直空間。 – Jasper 2014-09-28 12:24:00