0

I am receiving an exception which implies a problem with my inheritance structure, but cannot figure out the problem.

import tkinter as tk
class Game(tk.Tk):
class Period(tk.Frame, Game):
class PeriodSummary(tk.Frame,Period):

This gives the excpetion:

Traceback (most recent call last):
  File "tkinter_test.py", line 4, in <module>
    class PeriodSummary(tk.Frame,Period):
TypeError: Cannot create a consistent method resolution
order (MRO) for bases Frame, Period

So I want to have Period to inherit attributes from Game, and PeriodSummary to inherit attributes from Period. Why is this not possible?

3
  • 1
    It looks like you have a fundamental misunderstanding of how inheritance works. Why does PeriodSummary need to inherit from these other classes? Why are s a Period also a Game instead of being part of a game? What do you think that accomplishes? Commented Dec 2, 2016 at 3:15
  • A Game has many Periods. After each Period is played, I would like there to be a PeriodSummary frame come up. Period inherits from Game because it needs attributes from it, but has also differet attributes. Commented Dec 2, 2016 at 3:42
  • Read what you just wrote: a game has many periods. Each period is not also a game, which is what your inheritance is saying. One Game object should have several Period objects as attributes. You don't want to use inheritance just because you need to share data, that's not what inheritance is for. Commented Dec 2, 2016 at 3:51

1 Answer 1

2

You shouldn't inherit from both Frame and Tk. Those are very different things, and inheriting from both simply won't work as you expect. Plus, a tknter app should always only have a single instance of Tk.

You also shouldn't inherit from Frame and also from some other class that inherits from Frame.

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

2 Comments

But why does it not work? Is there something I can adjust?
@splinter: it doesn't work because it isn't designed to work. This is simply the wrong way to use tkinter. An object can't be both a frame and a root window at the same time.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.