Python Forum
Canvas not placing inside Grid
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Canvas not placing inside Grid
#1
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
Reply
#2
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.
Reply
#3
That's it. Thank you so much!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] Placing a Placeholder in the entry box Nymphalys08 1 1,935 Jun-21-2021, 06:43 AM
Last Post: Yoriz
  [Tkinter] Draw a grid of Tkinter Canvas Rectangles MrTim 5 7,904 May-09-2021, 01:48 PM
Last Post: joe_momma
  align frame inside canvas ro_btz 1 1,836 Sep-11-2020, 04:38 PM
Last Post: Larz60+
  [Tkinter] Resizing image inside Canvas (with Canvas' resize) Gupi 2 25,096 Jun-04-2019, 05:05 AM
Last Post: Gupi

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020