我无法让 canvas 按我想要的方式工作

I cant get the canvas to work how i wanted it to

我为我正在处理的设计创建了一个 GUI,canvas 正在新的 window 中弹出。我正在尝试让它出现在按钮下方。

__author__ =  'Isaac Alderton'
__authorEmail__ =  'Isaacary92@gmail.com'
from Tkinter import *

class Application(Frame):
""" My smart board application"""
def __init__(self, master):
    Frame.__init__(self, master)
    self.grid()
    self.create_widgets()

def create_widgets(self):

#Button1
#buttons ment to span across the width of the page
#needs linking to google calander
    self.button1 = Button(self, text = "        Calandar        ")
    self.button1.grid(row=1,column = 1)

#button2
    self.button2 = Button(self, text = "        weather        ")
    self.button2.grid(row=1,column = 2)

#button3
#defult home menu in the future user sets a new home
    self.button3 = Button(self, text = "        Whiteboard        ")
    self.button3.grid(row=1,column = 3)

#button4
    self.button4 = Button(self, text = "        Internet        ")
    self.button4.grid(row=1,column = 4)

#button5
    self.button5 = Button(self, text = "        Settings        ")
    self.button5.grid(row=1,column = 5)
#def settings():
    #pop up window where the settings options have wifi screen brightness     sleep options
    #and also account settings
    #One day also ui changes so color themes etc

#Canvas
#canvas ment to be part of whiteboard screen not a "pop-up"
#canvas is ment to fill the whole page


def xy(event):
    global lastx, lasty
    lastx, lasty = event.x, event.y

def addLine(event):
    global lastx, lasty
    canvas.create_line((lastx, lasty, event.x, event.y))
    lastx, lasty = event.x, event.y

root = Tk()
root.columnconfigure(0, weight=1)
root.rowconfigure(0, weight=1)

canvas = Canvas(root)
canvas.grid(column=5, row=4,)
canvas.bind("<Button-1>", xy)
canvas.bind("<B1-Motion>", addLine)





#endof canvas
root = Tk()
root.title("Smartboard")
#Geometry is ment to fit device????

root.geometry("530x500")
app = Application(root)

root.mainloop()


 #  Copyright {2015} {Isaac Alderton}

你的问题是代码的最后一部分是:

root = Tk()
#...
canvas = Canvas(root)
#...
#endof canvas
root = Tk()
#...
app = Application(root)

您正在创建一个 window,添加您 canvas,然后创建一个新的 window,并将框架添加到新的 window。如果你注释掉第二个 root = Tk(),它工作正常。调整布局为:

__author__ =  'Isaac Alderton'
__authorEmail__ =  'Isaacary92@gmail.com'
from Tkinter import *

class Application(Frame):
    """ My smart board application"""
    def __init__(self, master):
        Frame.__init__(self, master)
        self.grid(column=0, row=0, sticky="ew")
        self.create_widgets()
        for i in range(5):
            self.columnconfigure(i, weight=1)
        self.rowconfigure(0, weight=1)

    def create_widgets(self):

    #Button1
    #buttons ment to span across the width of the page
    #needs linking to google calander
        self.button1 = Button(self, text = "        Calandar        ")
        self.button1.grid(row=0, column = 0, sticky="ew")

    #button2
        self.button2 = Button(self, text = "        weather        ")
        self.button2.grid(row=0, column = 1, sticky="ew")

    #button3
    #defult home menu in the future user sets a new home
        self.button3 = Button(self, text = "        Whiteboard        ")
        self.button3.grid(row=0, column = 2, sticky="ew")

    #button4
        self.button4 = Button(self, text = "        Internet        ")
        self.button4.grid(row=0, column = 3, sticky="ew")

    #button5
        self.button5 = Button(self, text = "        Settings        ")
        self.button5.grid(row=0, column = 4, sticky="ew")
    #def settings():
        #pop up window where the settings options have wifi screen brightness     sleep options
        #and also account settings
        #One day also ui changes so color themes etc

#Canvas
#canvas ment to be part of whiteboard screen not a "pop-up"
#canvas is ment to fill the whole page


def xy(event):
    global lastx, lasty
    lastx, lasty = event.x, event.y

def addLine(event):
    global lastx, lasty
    canvas.create_line((lastx, lasty, event.x, event.y))
    lastx, lasty = event.x, event.y

root = Tk()
root.columnconfigure(0, weight=1)
root.rowconfigure(1, weight=1)

canvas = Canvas(root)
canvas.grid(column=0, row=1, sticky="nesw")
canvas.bind("<Button-1>", xy)
canvas.bind("<B1-Motion>", addLine)





#endof canvas
#root = Tk()
root.title("Smartboard")
#Geometry is ment to fit device????

root.geometry("530x500")
app = Application(root)

root.mainloop()


 #  Copyright {2015} {Isaac Alderton}

给出: