3

In the below example, I am trying to use Frame1() in MainW(). I tried many variations of the below code. The problem is that the frame object color and rowspan are not changing at all. I know there is a problem in terms of using Frame1() in MainW(). Can someone point out the error?

from tkinter import *

class Frame1(Frame):
        def __init__(self, parent):
            Frame.__init__(self, parent, bg="red")
            self.parent = parent
            self.widgets()
        def widgets(self):
            self.text = Text(self)
            self.text.insert(INSERT, "Hello World\t")
            self.text.insert(END, "This is the first frame")
            self.text.grid(row=0, column=0)


class MainW(Tk):
    def __init__(self, parent):
        Tk.__init__(self, parent)
        self.parent = parent
        self.mainWidgets()
    def mainWidgets(self):
        self.label = Label(self, text="Main window label")
        self.label.grid(row=0, column=0)
        self.window = Frame1(self)
        self.window.grid(row=0, column=10, rowspan=2)

if __name__=="__main__":
    app = MainW(None)
    app.mainloop()

Here is the output which is not what I want. I need the frame's background red and rowspan to be 1: enter image description here

Thank you

2
  • 1
    what color ? red ? You put Text() and you can't see frame color. Text() isn't transparent - it has own color. Commented Jan 28, 2016 at 1:48
  • 1
    what problem do you have with grid ? Cell in grid has no width and height till you put widget in this cell. Commented Jan 28, 2016 at 1:51

1 Answer 1

4

You can't see frame color because you put widget which fills all frame.

If you add margins (padx, pady) then you can see frame color.

self.text.grid(row=0, column=0, padx=20, pady=20)

You can't see rowspan because you have empty cell is second row. Empty cell has no width and height. Add Label in second row and will see how rowspan works.

from tkinter import *

class Frame1(Frame):
    def __init__(self, parent):
        Frame.__init__(self, parent, bg="red")
        self.parent = parent
        self.widgets()

    def widgets(self):
        self.text = Text(self)
        self.text.insert(INSERT, "Hello World\t")
        self.text.insert(END, "This is the first frame")
        self.text.grid(row=0, column=0, padx=20, pady=20) # margins


class MainW(Tk):

    def __init__(self, parent):
        Tk.__init__(self, parent)
        self.parent = parent
        self.mainWidgets()

    def mainWidgets(self):

        self.label1 = Label(self, text="Main window label", bg="green")
        self.label1.grid(row=0, column=0)

        self.label2 = Label(self, text="Main window label", bg="yellow")
        self.label2.grid(row=1, column=0)

        self.window = Frame1(self)
        self.window.grid(row=0, column=10, rowspan=2)

if __name__=="__main__":
    app = MainW(None)
    app.mainloop()

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.