0

I have an idea to create a simple scoreboard app (sport) with two windows (creating in Tkinter). One is for controlling and another one for output information.

So the idea is that I press button "show scoreboard" in a tk.Toplevel window and it appears in the main app window. And as I press hide, it hides. I know that I can create it just writing script without Classes and just like hundreds def strings but I want to use OOP as I would like to start programming the right way.

My problem is that when I press "Hide the scoreboard" (I create Labels for scoreboard) label is not hiding. Any suggestions?

I know that commands and defs must be in the same "tree", but how to arrange it when using the OOP.

So here is my code

import tkinter as tk

def forget():
scoreboard.pack_forget()

class Main(tk.Tk):
    def __init__(self):
        super().__init__()
        self.title("Scorebug")
        self.geometry("500x300")
        self.configure(background="green")
        scoreboard = tk.Label(self, text="This is like scoreboard")
        scoreboard.pack()

class Control(tk.Toplevel):
    def __init__(self):
        super().__init__()
        self.title("Controls")
        self.geometry("100x300")
        self.configure(background="red")

        hidelabels = tk.Button(self, text="Hide the scoreboard", command=forget)
        hidelabels.pack()


app = Main()
ctr = Control()

ctr.mainloop()
app.mainloop()
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • a good thing to learn is how to use the debugger, good answers for debugger here, https://stackoverflow.com/questions/4929251/how-to-step-through-python-code-to-help-debug-issues. As a first step see if you can use the debugger to check the line hidelabels = tk.Button ... is actually being invoked when you run the code – Nigel Savage Jan 23 '20 at 22:37
  • `tkinter` should run only one `mainloop()` – furas Jan 24 '20 at 03:55
  • your `scoreboard` is local variable and exists only when Python runs `__Init__` but later it removes this variable. You should use `self.` to keep access to `self.scoreboard.` You can send main window to second window as parameter - `Control(app)` and then you have access to main window, – furas Jan 24 '20 at 03:58

1 Answers1

0

First you should use self.scoreboard to have access from other places.

self.scoreboard = tk.Label(self, text="This is like scoreboard")
self.scoreboard.pack()

and now you can remove it using

command=app.scoreboard.pack_forget

You can also send main window as argument to second window

ctr = Control(app)

class Control(tk.Toplevel):
    def __init__(self, parent):

and then you can bind

command=parent.scoreboard.pack_forget

import tkinter as tk

class Main(tk.Tk):
    def __init__(self):
        super().__init__()
        self.title("Scorebug")
        self.geometry("500x300")
        self.configure(background="green")
        self.scoreboard = tk.Label(self, text="This is like scoreboard")
        self.scoreboard.pack()

class Control(tk.Toplevel):
    def __init__(self, parent):
        super().__init__()
        self.title("Controls")
        self.geometry("100x300")
        self.configure(background="red")

        hidelabels = tk.Button(self, text="Hide the scoreboard", command=parent.scoreboard.pack_forget)
        hidelabels.pack()


app = Main()
ctr = Control(app)
app.mainloop()

EDIT: You can also send only scireboard as argument to second window

ctr = Control(app.scoreboard)

and then you can bind

command=parent.pack_forget
furas
  • 134,197
  • 12
  • 106
  • 148
  • Thank you so much, This is what I needed. I broke my head trying to find out what I am doing wrong. I'm learning python for a month or so I'm a newbie. – Ravil Abbasov Jan 24 '20 at 21:04