0

I bolded the parts where I think the problem occurs. When run, the program stops and does not do anything but continues to run. This is a part of a bigger project to try to code minesweeper. I put ###HERE### on places where the problem occurs. ‏‏‎ ‎ ‏‏‎ ‎ ‏‏‎ ‎ ‏‏‎ ‎ ‏‏‎ ‎ ‏‏‎ ‎ ‏‏‎ ‎ ‏‏‎ ‎‏‏‎ ‎ ‏‏‎ ‎ ‏‏‎ ‎ ‏‏‎ ‎‏‏‎ ‎ ‏‏‎ ‎

from tkinter import *
from tkinter import Canvas
from PIL import ImageTk, Image
from time import sleep


class ResizingCanvas(Canvas):
    def __init__(self,parent,**kwargs):
        Canvas.__init__(self,parent,**kwargs)
        self.bind("<Configure>", self.on_resize)
        self.height = self.winfo_reqheight()
        self.width = self.winfo_reqwidth()

    def on_resize(self,event):
        # determine the ratio of old width/height to new width/height
        wscale = float(event.width)/self.width
        hscale = float(event.height)/self.height
        self.width = event.width
        self.height = event.height
        # resize the canvas
        self.config(width=self.width, height=self.height)
        # rescale all the objects tagged with the "all" tag
        self.scale("all",0,0,wscale,hscale)


class Minesweeper(Tk):

    def __init__(self, master):
        Tk.__init__(self)
        fr = Frame(self)
        fr.pack(fill=BOTH, expand=YES)
        self.canvas = ResizingCanvas(fr, width=940, height=920, bg="black", highlightthickness=0)
        self.canvas.pack(fill=BOTH, expand=YES)
        self.count = 0
        self.start = 0
        self.newWindow = Toplevel(self.master) ####HERE###
        self.app = Control(self.newWindow)     ####HERE###
        self.title("MineSweeper")
        x1 = 20
        y1 = 20
        x2 = 80
        y2 = 80
        self.block_pic = PhotoImage(file='C:/Users/akiva/OneDrive/Desktop/block.PNG')
        self.flag_pic = PhotoImage(file='C:/Users/akiva/OneDrive/Desktop/flag.PNG')
        for k in range(14):
            for i in range(15):
                self.canvas.create_rectangle(x1, y1, x2, y2, fill='white')
                x1 += 60
                x2 += 60
            x1 = 20
            x2 = 80
            y1 += 60
            y2 += 60

    def shift_image(self):
        if self.count == 0:
            Tk.canvas.itemconfig(self.block_pic, image=self.flag_pic)

    def end(self):
        del self.block_pic
        print("Game has ended")
        self.after(2000, quit())
        print("Game has ended")
        self.start = 0

    def frame(self):
        self.start += 1
        if self.start == 1:
            x1 = 50
            y1 = 50
            for i in range(14):
                for k in range(15):
                    self.canvas.create_image(x1, y1, image=self.block_pic)
                    x1 += 60
                x1 = 50
                y1 += 60
            self.canvas.pack()
        else:
            print("Game has already started")


class Control:
    def __init__(self, master):
        self.master = master
        self.frame = Frame(self.master)
        **start_button = Button(self.frame, text="Start Game", command=Minesweeper(Tk).frame(),)  ####HERE###
        stop_button = Button(self.frame, text="End Game", command=Minesweeper(Tk).end())     ####HERE###
        start_button.pack()
        stop_button.pack()
        self.quitButton = Button(self.frame, text='Quit', width=25, command=self.close_windows)
        self.quitButton.pack()
        self.frame.pack()

    def close_windows(self):
        self.master.destroy()







if __name__ == "__main__":
    root = Tk
    window = Minesweeper(root)
    root.mainloop()

Traceback (most recent call last):
  File "C:/Users/akiva/PycharmProjects/helloOpencv/Mine_sweeper.py", line 107, in <module>
    window = Minesweeper(root)
  File "C:/Users/akiva/PycharmProjects/helloOpencv/Mine_sweeper.py", line 37, in __init__
    self.app = Control(self.newWindow)
  File "C:/Users/akiva/PycharmProjects/helloOpencv/Mine_sweeper.py", line 86, in __init__
    start_button = Button(self.frame, text="Start Game", command=Minesweeper(Tk).frame(),)
  File "C:/Users/akiva/PycharmProjects/helloOpencv/Mine_sweeper.py", line 37, in __init__
    self.app = Control(self.newWindow)
  File "C:/Users/akiva/PycharmProjects/helloOpencv/Mine_sweeper.py", line 86, in __init__
    start_button = Button(self.frame, text="Start Game", command=Minesweeper(Tk).frame(),)
  File "C:/Users/akiva/PycharmProjects/helloOpencv/Mine_sweeper.py", line 29, in __init__
    Tk.__init__(self)
Ak1va
  • 13
  • 5
  • 1
    Can you please elaborate your error here – Ujjwal Dash Aug 03 '20 at 22:46
  • 2
    Please [edit] your question and at the error you are getting — the entire tracetback would be best. – martineau Aug 03 '20 at 22:47
  • Also, see http://stackoverflow.com/q/5767228/7432. As a bit of advice, create functions for your buttons, rather than trying to cram code into a lambda. Lambda has its place, but it makes debugging more difficult. – Bryan Oakley Aug 03 '20 at 23:12
  • Your code needs a lot of correction and rework, a lot doesn't work. Like `root = Tk` should be `root = Tk()` and here `command=Minesweeper(Tk).frame()` you are passing `Tk` class to master argument of `Minesweeper` which is causing `RecursionError` – Saad Aug 04 '20 at 08:32

1 Answers1

1

Like I said here, code needs many fixes and probably rework in a more organized way.

In your Control class, you are creating new instances of Minesweeper class and passing Tkinter's Tk class (not instance) to the parameter "master" which is causing RecursionError: maximum recursion depth exceeded in comparison. You need to call frame and end functions from the instance of Minesweeper class. A simple fix to this issue can be to pass self as an argument to the Control class.

In Minesweeper,

self.app = Control(self, self.newWindow)

In Control,

...
    def __init__(self, weeper, master):
        self.master = master
        self.frame = Frame(self.master)
        start_button = Button(self.frame, text="Start Game", command=weeper.frame)
        stop_button = Button(self.frame, text="End Game", command=weeper.end)
...

Also, don't forget to put parenthesis here root = Tk().

Saad
  • 3,340
  • 2
  • 10
  • 32