Tkinter Canvas小部件没有高度或宽度属性

时间:2018-03-28 17:06:35

标签: python canvas tkinter tkinter-canvas

我找到了Canvas小部件属性列表:http://infohost.nmt.edu/tcc/help/pubs/tkinter/web/canvas.html

在页面顶部显示“Tkinter 8.5 reference ...”。我使用IDLE导入Tkinter。 1)Tkinter.TkVersion返回8.5 2)Tkinter.TclVersion返回8.5

我在代码中定义了一个Canvas小部件:     surf_plot = Canvas(root,bg = ocean)

然后我做了:     root.update()

在另一个函数中,我尝试获取画布的宽度和高度。     draw_grid_lines(surf_plot)

def draw_grid_lines(widget):

    width = widget.width()
    height = widget.height()

程序失败了:

AttributeError:Canvas实例没有属性'width' AttributeError:Canvas实例没有属性'height'

我使用IDLE仔细检查了Canvas方法和属性列表(Netbeans不会向我展示这些)。这些属性都不会出现。

from Tkinter import *

print str(TkVersion)
print str(TclVersion)

def gui_quit():
    root.quit()



def define_frames():

    ocean = '#C0C0C0'
    surf_plot = Canvas(root,bg=ocean)

    func_cmd = Frame(root,bg='gray')
    std_cmd = Frame(root,bg='gray')

    root.grid_rowconfigure(0,weight=1)
    root.grid_rowconfigure(1,weight=1)
    root.grid_columnconfigure(0,weight=2)
    root.grid_columnconfigure(1,weight=2)
    root.grid_columnconfigure(2,weight=2)
    surf_plot.grid(row=0,column=0,columnspan=3,rowspan=2,sticky="nsew")
    root.update()
    draw_grid_lines(surf_plot)
    func_cmd.grid(row=0,column=3,columnspan=1,rowspan=1,sticky="nsew")
    std_cmd.grid(row=1,column=3,columnspan=1,rowspan=1,sticky="nsew")

    func_lab = Label(func_cmd,relief=RIDGE,width=40,text="Function/Unit Commands")
    std_lab = Label(std_cmd,relief=RIDGE,width=40,text="Standard Commands")

    func_lab.grid(row=0,column=0)
    std_lab.grid(row=0,column=0)


def define_global_buttons():
    print "Inside define buttons"
    root.bind_all('<KeyPress-F4>',get_out)
    root.bind_all('<KeyPress-F12>',toggle_grid_lines)
    root.grid()

def draw_grid_lines(widget):

    width = widget.width()
    height = widget.height()
    print "We need to draw grid lines on a canvas that is "+str(width)+" wide and "+str(height)+" high"

def get_out(event):
    print "We are out of here!"
    gui_quit()

def toggle_grid_lines(event):
    global grid_lines_active

    print "Inside toggle_grid_lines"
    if grid_lines_active:
        print "We are turning off grid lines"
        grid_lines_active = False
    else:
        print "We are turning on grid lines"
        grid_lines_active = True


grid_lines_active = False

root = Tk()
root.grid()
root.attributes("-fullscreen", True)
root.configure(background = 'black')
hgt = root.winfo_screenheight()
wdt = root.winfo_screenwidth()

if __name__ == "__main__":
    print "Window height = "+str(hgt)
    print "Window width = "+str(wdt)

    define_frames()
    define_global_buttons()

    root.mainloop()

0 个答案:

没有答案