Python Forum

Full Version: Canvas not placing inside Grid
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I am trying to build a fairly simple GUI that will allow me to use GPIO pins on a Raspberry PI 3. I am using grid to place the labels, buttons and indicators on the gui. I want the indicators to look like LEDs. I'm trying to use canvas (and Oval) to create the indicators. I can't get the Canvas items to appear inside the grid. It places them outside (as if creating a new grid).
Here's the code snippet:

from tkinter import *
import time
import RPi.GPIO as GPIO

LedPin1 = 11    # pin11
LedPin2 = 12    # pin12
Switch1 = 15
Switch2 = 16

class Application(Frame):
    """MyeVault BU Box Controller"""
    def __init__(self, master):
        super(Application, self).__init__(master)
        self.grid()
        self.create_widgets()

    def create_widgets(self):
        """Create widgets"""
        Label(self, text="Actions").grid(row = 0, column = 0)
        self.canvasPwr=Canvas(root, width=20, height=20, background='yellow')
        self.canvasPwr.grid(row=2,column=1)
        canvasLED=Canvas(root, width=20, height=20, background='green')
        canvasLED.grid(row=4,column=1)
        Label(self, text="Status").grid(row=0, column = 1)
        PowerLED = Label(self, text="Power LED", bg ="yellow")
        PowerLED.grid(row=1, column = 1)
        Label(self, text="HDD Activity").grid(row=3, column = 1)
        
        self.button_reset = Button(self)
        self.button_reset["text"] = "Reset"
        #self.button_reset (bg ="white")
        self.button_reset["command"] = self.Reset
        self.button_reset.grid(row = 1, column = 0)

        self.button_tapPower = Button(self)
        self.button_tapPower["text"]= "Tap Power Button"
        self.button_tapPower["command"]= self.tapPower
        self.button_tapPower.grid(row = 2, column = 0)

        self.button_holdPower = Button(self)
        self.button_holdPower["text"]= "Hold Power Button"
        self.button_holdPower["command"]= self.holdPower
        self.button_holdPower.grid(row = 3, column = 0)
    
and here's the result:
[Image: gui.JPG]

Much Appreciation for assistance!
Leon
The first thing you do when you instantiate your class is create a frame.
When you create your canvas, you use root as the parent. Change that to self
which is the frame. The root window is underneath the frame, and not visible.
That's it. Thank you so much!